FluentValidation:将值与值的总和进行比较?

时间:2019-02-19 16:05:35

标签: c# fluentvalidation

在为单个类的属性制定简单规则时,我发现FluentValidation很简单,但是一旦我需要比较集合中的值(例如List<T>属性),它就会变得非常冗长。假设以下两个最小类:

public class PurchaseOrder
{
    public List<LineItem> LineItems { get; set; }
    public decimal Total { get; set; }

    public PurchaseOrder()
    {
        LineItems = new List<LineItem>();
    }
}

public class LineItem
{
    public decimal Price { get; set; }
}

这堂课

class Program
{
    static void Main(string[] args)
    {
        PurchaseOrder order = new PurchaseOrder();
        order.LineItems.Add(new LineItem() { Price = 12m });
        order.LineItems.Add(new LineItem() { Price = 14m });
        order.Total = 26m;

        PurchaseOrderValidator validator = new PurchaseOrderValidator();
        ValidationResult result = validator.Validate(order);
    }
}

如何PurchaseOrderValidator来确保所有LineItem.Price的总和等于PurchaseOrder.Total?这是一个存根(尽管我不确定Must()是要走的路):

public class PurchaseOrderValidator : AbstractValidator<PurchaseOrder>
{
    public PurchaseOrderValidator()
    {
        RuleFor(x => x.Total).Must(MatchSumOfLineItems);
    }

    private bool MatchSumOfLineItems(decimal arg)
    {
        return true;
    }
}

2 个答案:

答案 0 :(得分:3)

只需创建将PurchaseOrder作为输入的验证方法,然后使用它来做您想做的事

public class PurchaseOrderValidator : AbstractValidator<PurchaseOrder>
{
    public PurchaseOrderValidator()
    {
        RuleFor(x => x).Must(MatchSumOfLineItems);
    }

    private bool MatchSumOfLineItems(PurchaseOrder arg)
    {
        return arg.Total == arg.LineItems.Sum(i => i.Price);
    }
}

或者如果您想专门为Total属性添加验证,则可以使用另一个Must重载接受Func<decimal, PurchaseOrder, bool>

public class PurchaseOrderValidator : AbstractValidator<PurchaseOrder>
{
    public PurchaseOrderValidator()
    {
        RuleFor(x => x.Total).Must(MatchSumOfLineItems);
    }

    private bool MatchSumOfLineItems(PurchaseOrder order, decimal sum)
    {
        return sum == order.LineItems.Sum(i => i.Price);
    }
}

答案 1 :(得分:2)

为什么不使用.Equal()

public class PurchaseOrderValidator : AbstractValidator<PurchaseOrder>
{
    public PurchaseOrderValidator()
    {
        RuleFor(x => x.Total).Equal(x => x.LineItems.Sum(item => item.Price));
    }
}