在FluentValidation中是否有扩展或其他方式来推迟选择子验证器,具体取决于要验证的属性的类型/值?
我的情况是我有一个我要验证的Notification类。此类具有Payload属性,该属性可以是多种Payload类型之一,例如SmsPayload,EmailPayload等。这些Payload子类中的每一个都有自己的相关验证器,例如:分别为SmsPayloadValidator和EmailPayloadValidator。除上述内容外,核心库和个别通知提供者均未提及。从本质上讲,这意味着我可以根据需要添加提供程序,并使用IoC连接所有内容。
考虑以下课程:
public class Notification
{
public Payload Payload { get; set; }
public IEnumerable<string> Details { get; set; }
}
public abstract class Payload
{
public string Message { get; set; }
public abstract string Type { get; }
}
public class SmsPayload : Payload
{
public List<string> Numbers { get; set; }
public string Region { get; set; }
public string Provider { get; set; }
}
有一个Notification验证器和SmsPayloadValidator,如下所示:
public class NotificationValidator : AbstractValidator<Notification>
{
public NotificationValidator(IValidator<Payload> payloadValidator)
{
RuleFor(notification => notification.Payload).NotNull().WithMessage("Payload cannot be null.");
RuleFor(notification => notification.Payload).SetValidator(payloadValidator);
}
}
public class SmsPayloadValidator : AbstractValidator<SmsPayload>
{
public SmsPayloadValidator()
{
RuleFor(payload => payload.Provider)
.Must(s => !string.IsNullOrEmpty(s))
.WithMessage("Provider is required.");
RuleFor(payload => payload.Numbers)
.Must(list => list != null && list.Any())
.WithMessage("Sms has no phone numbers specified.");
RuleFor(payload => payload.Region)
.Must(s => !string.IsNullOrEmpty(s))
.WithMessage("Region is required.");
}
}
正如我所提到的,NotificationValidator所在的程序集不引用各个Payload验证程序类所在的程序集。所有接线都由Ioc(此项目的简单注入器)处理。
基本上我想做类似以下的事情 - 首先在Simple Injector中注册工厂回调:
container.Register<Func<Payload, IValidator<Payload>>>(() => (payload =>
{
if (payload.GetType() == typeof(SmsPayload))
{
return container.GetInstance<ISmsPayloadValidator>();
}
else if (payload.GetType() == typeof(EmailPayload))
{
return container.GetInstance<IEmailPayloadValidator>();
}
else
{
//something else;
}
}));
这样我可以按如下方式选择合适的验证器:
public class NotificationValidator : AbstractValidator<Notification>
{
public NotificationValidator(Func<Payload, IValidator<Payload>> factory)
{
RuleFor(notification => notification.Payload).NotNull().WithMessage("Payload cannot be null.");
RuleFor(notification => notification.Payload).SetValidator(payload => factory.Invoke(payload));
}
}
有什么建议吗?还是有更好的方法来做我提出的建议? 如果没有,我将分叉FluentValidation存储库并提交PR。
答案 0 :(得分:5)
你可以通过避开工厂来使你的意图更加清晰。虽然最终结果可能与此方法相同,但您至少可以直接注入IValidator<Payload>
而不是Func<Payload, IValidator<Payload>>
。
创建一个名为PolymorphicValidator
的类。这将允许您以一致的方式重复此模式,并且如果您愿意,还可以提供后备基本验证器。这基本上是Simple Injector文档中描述here的推荐“复合模式”。
public class PolymorphicValidator<T> : AbstractValidator<T> where T : class
{
private readonly IValidator<T> _baseValidator;
private readonly Dictionary<Type, IValidator> _validatorMap = new Dictionary<Type,IValidator>();
public PolymorphicValidator() { }
public PolymorphicValidator(IValidator<T> baseValidator)
{
_baseValidator = baseValidator;
}
public PolymorphicValidator<T> RegisterDerived<TDerived>(IValidator<TDerived> validator) where TDerived : T
{
_validatorMap.Add(typeof (TDerived), validator);
return this;
}
public override ValidationResult Validate(ValidationContext<T> context)
{
var instance = context.InstanceToValidate;
var actualType = instance == null ? typeof(T) : instance.GetType();
IValidator validator;
if (_validatorMap.TryGetValue(actualType, out validator))
return validator.Validate(context);
if (_baseValidator != null)
return _baseValidator.Validate(context);
throw new NotSupportedException(string.Format("Attempted to validate unsupported type '{0}'. " +
"Provide a base class validator if you wish to catch additional types implicitly.", actualType));
}
}
然后您可以像这样注册您的验证器(可选择提供基类回退和其他子类验证器):
container.RegisterSingle<SmsPayloadValidator>();
//container.RegisterSingle<EmailPayloadValidator>();
container.RegisterSingle<IValidator<Payload>>(() =>
new PolymorphicValidator<Payload>(/*container.GetInstance<PayloadValidator>()*/)
.RegisterDerived(container.GetInstance<SmsPayloadValidator>())
/*.RegisterDerived(container.GetInstance<EmailPayloadValidator>() */);
这将创建一个单身PolymorphicValidator
,其中包含单身儿童验证器(FluentValidation团队推荐单身人士)。您现在可以注入IValidator<Payload>
,如第一个NotificationValidator
示例所示。
public class NotificationValidator : AbstractValidator<Notification>
{
public NotificationValidator(IValidator<Payload> payloadValidator)
{
RuleFor(notification => notification.Payload)
.NotNull().WithMessage("Payload cannot be null.")
.SetValidator(payloadValidator);
}
}
答案 1 :(得分:4)
我同意泰勒关于使用复合材料的答案(所以肯定是+1),但他的实施并不实用。因此,我建议稍微不同的实现,同时仍然使用复合。
如果我没弄错的话,你的复合材料应如下所示:
public class CompositeValidator<T> : AbstractValidator<T> where T : class
{
private readonly Container container;
public CompositeValidator(Container container)
{
this.container = container;
}
public override ValidationResult Validate(T instance)
{
var validators = this.container.GetAllInstances(instance.GetType());
return new ValidationResult(
from IValidator validator in validators
from error in validator.Validate(instance).Errors
select error);
}
}
注册应如下:
// Simple Injector v3.x
container.RegisterCollection(typeof(IValidator<>),
AppDomain.CurrentDomain.GetAssemblies());
container.Register(typeof(IValidator<>),
typeof(CompositeValidator<>),
Lifestyle.Singleton);
// Simple Injector v2.x
container.RegisterManyForOpenGeneric(
typeof(IValidator<>),
container.RegisterAll,
AppDomain.CurrentDomain.GetAssemblies());
container.RegisterOpenGeneric(
typeof(IValidator<>),
typeof(CompositeValidator<>),
Lifestyle.Singleton);
这里会发生以下情况:
RegisterCollection
调用可确保将所有验证程序注册为集合。这意味着对于每个T,其中可以有多个验证器。例如,如果您的系统有PayloadValidator
和SmsPayloadValidator
,则解析GetAllInstances<IValidator<SmsPayload>>
将返回两个验证程序,因为IValidator<in T>
包含in
关键字(是逆变) Register
注册会为所请求的每个CompositeValidator<T>
注册要返回的IValidator<T>
。由于Simple Injector differentiates registrations of collections with one-to-one registrations,注入IValidator<T>
将始终导致复合验证器被注入。由于复合验证器仅依赖于容器,因此可以将其注册为singleton。CompositeValidator<T>
(当他们依赖IValidator<T>
时),复合验证器将根据确切的类型要求验证者的集合。因此,如果消费者使用IValidator<Payload>
,复合验证器将确定实际类型(例如SmsPayload
)并请求此类型的所有可分配验证器,并将验证转发给这些类型。ValidationResult
。