FluentValidation如何创建公用部分

时间:2018-08-24 20:11:46

标签: c# fluentvalidation

我有一个抽象的类(无关紧要):

public abstract class CarrierAbstractFormAPI
{
    public string Name { get; set; }
    public string Fein { get; set; }
    public string McNumber { get; set; }
    public string DotNumber { get; set; }

    public AddressCreateAPI Address { get; set; }
}

和AddressCreateAPI类:

public class AddressCreateAPI
{
    public string Street { get; set; }
    public string City { get; set; }
    public string ZipPostalCode { get; set; }
    public int StateProvinceId { get; set; }
    public string ContactName { get; set; }
    public string ContactPhone { get; set; }
    public string ContactFaxNumber { get; set; }
    public string ContactEmail { get; set; }
}

我的验证者:

public abstract class CarrierAbstractFluentValidation<T> : AbstractValidator<T> where T : CarrierAbstractFormAPI
{
    public CarrierAbstractFluentValidation()
    {
        RuleFor(d => d.Name)
            .NotEmpty().WithMessage("Name is required");

        RuleFor(d => d.Fein)
            .NotEmpty().WithMessage("Fein is required");

        RuleFor(d => d.McNumber)
            .NotEmpty().WithMessage("McNumber is required");

        RuleFor(d => d.DotNumber)
            .NotEmpty().WithMessage("DotNumber is required");

        RuleFor(d => d.Address.Street)
            .NotEmpty().WithMessage("Address Street is required");

        RuleFor(d => d.Address.City)
            .NotEmpty().WithMessage("Address City is required");

        RuleFor(d => d.Address.StateProvinceId)
            .InclusiveBetween(0, int.MaxValue).WithMessage("Address State is required");

    }
}

工作正常。 但是我还有一些其他的类,可以有

public AddressCreateAPI Address { get; set; }

属性。 我要移动一部分:

        RuleFor(d => d.Address.Street)
            .NotEmpty().WithMessage("Address Street is required");

        RuleFor(d => d.Address.City)
            .NotEmpty().WithMessage("Address City is required");

        RuleFor(d => d.Address.StateProvinceId)
            .InclusiveBetween(0, int.MaxValue).WithMessage("Address State is required");

应用于普通类,并将其应用于我所有的流利的验证器中,该验证器具有Address属性。怎么做?

2 个答案:

答案 0 :(得分:2)

要考虑一下,您需要做的就是重用验证器类。

class AddressCreateAPIValidator : AbstractValidator<AddressCreateAPI>
{
    public AddressCreateAPIValidator()
    {
        RuleFor(d => d.Street)
        .NotEmpty().WithMessage("Address Street is required");

        RuleFor(d => d.City)
            .NotEmpty().WithMessage("Address City is required");

        RuleFor(d => d.StateProvinceId)
            .InclusiveBetween(0, int.MaxValue).WithMessage("Address State is required");
    }
}

class SomeClass
{
    public AddressCreateAPI Prop { get; set; }
}

class SomeClassValidator : AbstractValidator<SomeClass>
{
    public SomeClassValidator()
    {
        RuleFor(d => d.Prop).SetValidator(new AddressCreateAPIValidator());
    }
}

请注意,AddressCreateAPIValidator如何提取用于验证AddressCreateAPI类的通用逻辑,然后通过调用SetValidator将其重新用于属性。

如果要创建通用验证器,可以将其与其他答案中的基于反射的方法混合并匹配。

答案 1 :(得分:1)

您可以使用以下扩展方法,该方法通过反射检查传递的类型并应用指定的验证规则:

public static bool Apply<T, TProperty>(this AbstractValidator<T> validator, string propertyName, Action<IRuleBuilderInitial<T, TProperty>> rule)
{
    var property = typeof(T).GetProperty(propertyName);

    if (property == null)
    {
        Console.WriteLine($"{typeof(T).Name} does not expose property {propertyName}!");
        return false;
    }

    if (!typeof(TProperty).IsAssignableFrom(property.PropertyType))
    {
        Console.WriteLine($"Property {typeof(T).Name}.{propertyName} is of type {property.PropertyType.Name} which is not (derived from) {typeof(TProperty).Name}!");
        return false;
    }

    rule(validator.RuleFor(t => (TProperty)property.GetValue(t)));

    return true;
}

用法示例:

class a
{
    public string Prop { get; set; }
}

class b
{
    public DateTime Prop { get; set; }
}

class c
{
    public string Prop2 { get; set; }
}

class Validator<T> : AbstractValidator<T>
{
    public Validator()
    {
        this.Apply<T, string>("Prop", r => r.NotEmpty().WithMessage("Prop is required"));
    }
}

Console.WriteLine(new Validator<a>().Validate(new a { Prop = "AAA" }));
Console.WriteLine(new Validator<a>().Validate(new a()));
Console.WriteLine(new Validator<b>().Validate(new b { Prop = DateTime.Now }));
Console.WriteLine(new Validator<c>().Validate(new c { Prop2 = "AAA" }));
Console.WriteLine(new Validator<c>().Validate(new c { Prop2 = "AAA" }));