带有 FluentValidation 的 StructureMap
StructureMap with 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 继承验证器 class:
public class DefaultValidator : AbstractValidator<DefaultViewModel>
当我只有一个验证器 class 时,DI 工作,一旦我创建了第二个,它就会中断。
我也在使用程序集搜索为验证器注册所有实例:
services
.AddMvc()
.AddFluentValidation(x=>x.RegisterValidatorsFromAssemblyContaining<Startup>())
自定义验证器工厂的问题已解决。
实现了 FluentValidation 接口 IValidatorFactory 并注入了 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>());
我在 .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 继承验证器 class:
public class DefaultValidator : AbstractValidator<DefaultViewModel>
当我只有一个验证器 class 时,DI 工作,一旦我创建了第二个,它就会中断。
我也在使用程序集搜索为验证器注册所有实例:
services
.AddMvc()
.AddFluentValidation(x=>x.RegisterValidatorsFromAssemblyContaining<Startup>())
自定义验证器工厂的问题已解决。
实现了 FluentValidation 接口 IValidatorFactory 并注入了 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>());