. NET business entity verification component Fluent Validation

Source: Internet
Author: User
Tags nopcommerce

. NET business entity verification component Fluent Validation
Understand Fluent Vaidation. it is so simple to see that this formation is used in the NopCommerce project. It is a genius to separate data verification from the business entity class, it was later known that this was an open-source lightweight verification mechanism. Fluent Validation open-source Codeplex its home page introduction: this component is a lightweight.. NET class library, using FLUENT Interface Definition and lambda expressions to build A validation rule for the business class (A small validation library. NET that uses a fluent interface and lambda expression for building validation rules for you business objects .) this class library can be used not only in the asp.net mvc project, but also in common class libraries. Of course, it is also supported in the asp.net form project. How to use it: whether it is easy to use, but also whether it is as recommended on its official website. I like the examples on the official website. At a glance, I can see that the usage is definitely like its name and smoothness. This is also a common usage of interpreted language, an unlimited number of attribute extensions are supported for a type. Business Entity class: Copy code 1 public class Person 2 {3 public string NameField; 4 public int Id {get; set;} 5 public string Surname {get; set ;} 6 public string Forename {get; set;} 7 8 public List <Person> Children {get; set;} 9 public string [] NickNames {get; set ;} 10 11 public DateTime DateOfBirth {get; set;} 12 13 public int? NullableInt {get; set;} 14 15 public Person () 16 {17 Children = new List <Person> (); 18 Orders = new List <Order> (); 19} 20 21 public int CalculateSalary () 22 {23 return 20; 24} 25 26 public Address {get; set;} 27 public IList <Order> Orders {get; set;} 28 29 public string Email {get; set;} 30 public decimal Discount {get; set;} 31 public double Age {get; set;} 32 public int AnotherInt {ge T; set;} 33 34 public string CreditCard {get; set;} 35 36 public int? OtherNullableInt {get; set;} 37} 38 39 public interface IAddress 40 {41 string Line1 {get; set;} 42 string Line2 {get; set;} 43 string Town {get; set ;}44 string County {get; set ;}45 string Postcode {get; set ;}46 Country {get; set ;}47} 48 49 public class Address: IAddress 50 {51 public string Line1 {get; set;} 52 public string Line2 {get; set;} 53 public string Town {get; set ;} 54 public string County {get; set;} 55 public string Postcode {get; set;} 56 public Country {get; set;} 57 public int Id {get; set ;}58} 59 60 public class Country61 {62 public string Name {get; set ;}63} 64 65 public interface IOrder 66 {67 decimal Amount {get ;} 68} 69 70 public class Order: IOrder 71 {72 public string ProductName {get; set;} 73 public decimal Amount {get; set ;} 74} copy the code to specify the verification rules for Person: Copy code 1 using FluentValidation; 2 3 public class CustomerValidator: AbstractValidator <Customer> 4 {5 public CustomerValidator () 6 {7 RuleFor (customer => customer. surname ). notEmpty (); 8 RuleFor (customer => customer. forename ). notEmpty (). withMessage ("Please specify a first name"); 9 RuleFor (customer => customer. discount ). notEqual (0 ). when (customer => customer. hasDiscount ); 10 RuleFor (customer => customer. address ). length (20,250); 11 RuleFor (customer => customer. postcode ). must (BeAValidPostcode ). withMessage ("Please specify a valid postcode"); 12} 13 14 private bool BeAValidPostcode (string postcode) 15 {16 // custom postcode validating logic goes here17} 18} 19 20 // Manual verification rule 21 Customer customer Customer = new Customer (); 22 CustomerValidator validator = new CustomerValidator (); 23 V AlidationResult results = validator. validate (customer); 24 25 bool validationSucceeded = results. isValid; 26 IList <ValidationFailure> failures = results. how to integrate the replication code Flent validation with the asp.net mvc verification library? If this is used in asp.net mvc, many may not know about it. We know that the Asp.net MVC project has its own certification authority [enterprise database VAB (Validation Application Block ), attribute-based declarative verification], its usage method has been recognized by us all the time, but it also has a lot of inflexible, but Fluent Validation is indeed more flexible. It is easy to use and smooth, and the verification rules are a separate class, which is classified by industry-specific objects. We do not need Xiang VAB, you must use Attribute to register verification rules on the business entity class. Since it is not the default validation rule library of ASP. net mvc, We need to register it in the validation rule library of ASP. net mvc. Copy code 1 // register asp.net mvc as the default validation rule repository in the Applicaton_Start () function of Global. asax. cs. 2 3 // fluent validation4 FluentValidationModelValidatorProvider provider = new FluentValidationModelValidatorProvider (new AttributedValidatorFactory (); 5 ModelValidatorProviders. providers. add (provider); 6 7 DataAnnotationsModelValidatorProvider. addImplicitRequiredAttributeForValueTypes = false; copy the code Note: 1,) as the Fluent Validation verification rule class must inherit AbstractValidator <T>; 2,) We can also follow NopCommerce's processing method to AttributeValidator The Validator (Type type) function of the Factory class is rewritten to support other verification rules in special business environments.

Related Article

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.