gpt4 book ai didi

.net - 根据属性类型/值推迟子验证器的选择

转载 作者:行者123 更新时间:2023-12-02 08:52:13 25 4
gpt4 key购买 nike

在 FluentValidation 中,是否有扩展或其他方式可以根据正在验证的属性的类型/值来推迟子验证器的选择?

我的情况是我有一个要验证的通知类。该类有一个 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(此项目的简单注入(inject)器)负责。

基本上我想做类似以下的事情 - 首先通过在简单注入(inject)器中注册工厂回调:

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));
}
}

有什么建议吗?或者有更好的方法来做我提议的事情吗?如果没有,我将 fork FluentValidation 存储库并提交 PR。

最佳答案

通过避开工厂,您可能会更清楚地表达您的意图。虽然最终结果可能与此方法相同,但您至少可以最终注入(inject) IValidator<Payload>直接代替Func<Payload, IValidator<Payload>> .

创建一个名为 PolymorphicValidator 的类。这将允许您以一致的方式重复此模式,并且如果您愿意,还可以提供后备基础验证器。这本质上是推荐的“复合模式”描述here在简单注入(inject)器文档中。

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 团队推荐单例)。您现在可以注入(inject)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);
}
}

关于.net - 根据属性类型/值推迟子验证器的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28755636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com