asp.net中FluentValidation组件简单实例

作者:袖梨 2022-06-25

这里要介绍一款与企业库VAB(Validation Application Block),ASP.NET MVC基于Attribute声明式验证所不同的验证组件,FluentValidation,其利用表达式语法链式编程,使得验证组件与实体分开。我喜欢他的原因是喜欢表达式,喜欢链式的感觉,大有一气呵成之意。

    进入今天的主题首先如果你还没有这款组件,你可以利用VS2010的NuGet安装,(如果不会的请安装上篇开源DataBase组件:FluentMigrator中提到的方式安装),命令为:
未命名
 

  现在我们可以开始体验了,实体类do任然是上节的Orders:

public  class  Orders
   {
       public int ID { get; set; }

       public string CustomerID { get; set; }

       public decimal DisCount { get; set; }

       public DateTime OrderDate { get; set; }
   }

验证逻辑:

public  class  OrdersValidator:AbstractValidator
    {
        public  OrdersValidator()
        {
            RuleFor(orders => orders.CustomerID).NotEmpty().Length(2, 20).WithName("CustomerID");
            RuleFor(orders => orders.DisCount).GreaterThanOrEqualTo(0).LessThan(1).WithMessage("discount must between 0 and 1!");
            RuleFor(orders => orders.OrderDate.Date).GreaterThanOrEqualTo(DateTime.Now.Date).WithName("Order Date");
        }
    }

看见这样的代码你的感觉如何?现在式的语言讲究的不仅在于功能,还有语义。

下面我们来写个简单的测试类测试它:

[TestMethod]
       public void TestMethod1()
       {
           var orders = new Orders(){DisCount = 2,CustomerID = "1", OrderDate = DateTime.Now.AddDays(-1).Date};
           IValidator validator = new OrdersValidator();
           var results = validator.Validate(orders);

           var validationSucceeded = results.IsValid;
           var failures = results.Errors;
           Assert.IsTrue(failures.Any(t => t.PropertyName == "CustomerID"));
           Assert.IsTrue(failures.Any(t => t.PropertyName == "DisCount"));
           Assert.IsTrue(failures.Any(t => t.PropertyName == "OrderDate.Date"));
           failures.ToList().ForEach(t=>Debug.WriteLine(t.ErrorMessage));
       }

验证结果

单元测试结果就不贴了,觉得多余。

   最后忘说了这款组件为我们提供了很多语言的多语言支持:

   我们来看官网的一个实例

The main source code repository for this project is on GitHub although it is also mirrored to CodePlex.

NuGet packages are available - the package IDs are FluentValidation and FluentValidation.MVC3.

Example

 

 代码如下 复制代码

using FluentValidation;

public class CustomerValidator: AbstractValidator {
  public CustomerValidator() {
    RuleFor(customer => customer.Surname).NotEmpty();
    RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Please specify a first name");
    RuleFor(customer => customer.Company).NotNull();
    RuleFor(customer => customer.Discount).NotEqual(0).When(customer => customer.HasDiscount);
    RuleFor(customer => customer.Address).Length(20, 250);
    RuleFor(customer => customer.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode");
  }

  private bool BeAValidPostcode(string postcode) {
    // custom postcode validating logic goes here
  }
}

Customer customer = new Customer();
CustomerValidator validator = new CustomerValidator();
ValidationResult results = validator.Validate(customer);

bool validationSucceeded = results.IsValid;
IList failures = results.Errors;

相关文章

精彩推荐