Fluent verification component: FluentValidation

Source: Internet
Author: User

Here we will introduce a VAB (Validation Application Block), ASP.. net mvc uses the Attribute declarative verification component FluentValidation, which uses the expression syntax chain programming to separate the verification component from the entity. The reason why I like him is that I like expressions and chain.

Enter the topic of today. First, if you do not have this component, you can use NuGet of VS2010 to install it. If not, install the open source DataBase component: the method mentioned in FluentMigrator ), command:

Now we can start to experience it. The entity class do is still the Orders in the previous section:

Do:

 
 
  1. public  class  Orders   
  2.    {   
  3.        public int ID { get; set; }   
  4.        public string CustomerID { get; set; }   
  5.        public decimal DisCount { get; set; }   
  6.        public DateTime OrderDate { get; set; }   
  7.    } 

Verification logic:

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

How do you feel when you see this code? The current language focuses not only on functions but also semantics.

Next we will write a simple test class to test it:

 
 
  1. [TestMethod]   
  2.        public void TestMethod1()   
  3.        {   
  4.            var orders = new Orders(){DisCount = 2,CustomerID = "1", OrderDate = DateTime.Now.AddDays(-1).Date};   
  5.            IValidator validator = new OrdersValidator();   
  6.            var results = validator.Validate(orders);   
  7.  
  8.            var validationSucceeded = results.IsValid;   
  9.            var failures = results.Errors;   
  10.            Assert.IsTrue(failures.Any(t => t.PropertyName == "CustomerID"));   
  11.            Assert.IsTrue(failures.Any(t => t.PropertyName == "DisCount"));   
  12.            Assert.IsTrue(failures.Any(t => t.PropertyName == "OrderDate.Date"));   
  13.            failures.ToList().ForEach(t=>Debug.WriteLine(t.ErrorMessage));   
  14.        } 

Result:

The unit test results will not be posted, and I feel redundant.

Finally, I forgot to mention that this component provides us with multi-language support in many languages:

I am thinking that the verification components we adopt in architecture design can be switched at will. We can create a facade mode for the same abstract interface of VAB and FluentValidation. With the help of the IOC plug-in architecture, different keys are used to obtain the verification component interface, and this key value will be processed on the Attribute of the method, and the AOP method is cross-cutting to our application, verification is a business function component, a cross-cutting point. I am referencing my architecture as well.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.