c# - 带有 FluentValidation 的 StructureMap

标签 c# asp.net-core fluentvalidation

我在 .net 核心项目中使用结构图作为 DI 框架。 我注册的fluentValidation如下:

public class DefaultRegistry : Registry
{
    public DefaultRegistry()
    {
        Scan(o =>
        {
            o.AssemblyContainingType<Startup>();
            o.AddAllTypesOf<IValidator>();
            o.LookForRegistries();
            o.AddAllTypesOf<Profile>();
            o.WithDefaultConventions();
        });
    }
}

抛出的错误: http://pastebin.com/eDHEcCfc

我从 AbstractValidator 继承验证器类:

public class DefaultValidator : AbstractValidator<DefaultViewModel>

当我只有一个验证器类时,DI 工作,一旦我创建了第二个,它就会中断。

我还使用程序集搜索为验证器注册所有实例:

services
                .AddMvc()
                .AddFluentValidation(x=>x.RegisterValidatorsFromAssemblyContaining<Startup>())

最佳答案

问题已通过自定义验证器工厂解决。

实现了FluentValidation接口(interface)IValidatorFactory并注入(inject)了StructureMap IContainer

using System;
using System.Reflection;
using FluentValidation;
using StructureMap;

namespace Business.Managers.Interfaces
{
    public class FluentValidatorFactory : IValidatorFactory
    {
        private readonly IContainer _container;

        public FluentValidatorFactory(IContainer container)
        {
            _container = container;
        }
        public IValidator<T> GetValidator<T>()
        {
            return (IValidator<T>)GetValidator(typeof(T));
        }

        public IValidator GetValidator(Type type)
        {
            IValidator validator;

            try
            {
                validator = CreateInstance(typeof(IValidator<>).MakeGenericType(type));
            }
            catch (Exception)
            {
                // Get base type and try to find validator for base type (used for polymorphic classes)
                var baseType = type.GetTypeInfo().BaseType;
                if (baseType == null)
                {
                    throw;
                }

                validator = CreateInstance(typeof(IValidator<>).MakeGenericType(baseType));
            }

            return validator;
        }

        public IValidator CreateInstance(Type validatorType)
        {
            return _container.GetInstance(validatorType) as IValidator;
        }
    }
}

还通过自定义服务解析正确的验证器:

using FluentValidation;
using FluentValidation.Results;

namespace Business.Managers.Interfaces
{
    public class ValidationManager : IValidationManager
    {
        private readonly IValidatorFactory _validatorFactory;

        public ValidationManager(IValidatorFactory validatorFactory)
        {
            _validatorFactory = validatorFactory;
        }

        public ValidationResult Validate<T>(T entity) where T : class
        {
            var validator = _validatorFactory.GetValidator(entity.GetType());
            var result = validator.Validate(entity);
            return result;
        }
    }
}

在 startup.cs 中使用 AddFluentValidation 注册验证器

services.AddMvc().AddFluentValidation(x=>x.RegisterValidatorsFromAssemblyContaining<LoansRequestValidator>());

关于c# - 带有 FluentValidation 的 StructureMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42028546/

相关文章:

c# - 将一个范围内的数字转换为另一个范围内的数字

asp.net-web-api - 如何在ConfigureServices方法中获取IOptions或将IOptions传递到扩展方法中?

asp.net-core - 如何添加所有 Razor 页面都可以访问的功能?

c# - FluentValidation 涉及正在验证的对象中的 2 个属性

asp.net-core - 如何访问 FluentValidation 构造函数内的模型属性?

c# - Entity Framework 4 和 Linq : OrderBy a field nested in a Query : refactor my code

c# - 当 outlook 的连接状态发生变化时触发的事件处理程序

c# - 在 .net core 中编写 windows 服务

c# - FluentValidation 命令验证器未由 AutoFac 注册

c# - .NET C# 套接字并发问题