. NET business entity class validation component fluent Validation

Source: Internet
Author: User
Tags nopcommerce

Meet the fluent vaidation.

  It's so easy to see this build in the Nopcommerce project, separating the data validation from the business entity class was a genius idea, and later learned that this thing was an open source lightweight validation build.   Fluent Validation translation: Fluent validation   Open source CodePlex Its homepage description: The component is a lightweight. NET class library that uses fluent interface definitions and lambda expressions to build a validation rule for a business class (a small Validation library for. NET, uses a fluent interface and lambda expression for building validation rules for you busin ESS objects.)   This class library is not only available in ASP. NET MVC projects, but also in common class libraries, which are also supported in the ASP. NET Form project.   How to use: Is not easy to use, but also to see if it is really like its official website suggested description. I prefer the example of its official online, one can see the usage of the feeling, definitely as its name, fluency, this is also a kind of interpretation of the common use of the language, unlimited support for a type of infinite attribute extension.        Business entity class:  copy Code  1 public class Person  2 { 3     public string N AMEFIELD;&NBSP;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;}       &NBSP;&NBSP;11     Public DateTime dateOfBirth {get; set;} 12&NBSP;13     public int? Nullableint {get; set;} 14 15     Public person ()  16     {$         children = new List<pers On> ()         Orders = new list<order> (),    }20     21     public int calculatesalary () ()     {20;24         return ( )  }25&NBSP;2 6     Public address address {get; set;}     Public ilist<order> Orders {get; set;} 28 29     public string Email {get; set;}     Public decimal Discount {get; set;}     Public double age {get; set;}     public int Anotherint {get; set;} 33 34     public string CreditCard {get; set;} 35 36     public int? Othernullableint {get; set;} PNS}38 39 public interface iaddress 40 {    String Line1 {get; set;}     string Line2 {get; set;}     string Town {get; set;}     String County {get; set;}     string Postcode {get; set;}     Country Country {get; set;} }48 49 public class Address:iaddress 50 {Wuyi     public string Line1 {get; set;}     public string Line2 {get; set;}     public string Town {get; set;}     public string County {get; set;}     public string postcode {get; set;}     Public country country {get; set;}     public int Id {get; set;} }59 60 public class Country61 {$     public string Name {get; set;} }64 65 public interface iorder 66 {    decimal Amount {get;} }69 70 public class Order:iorder 71 {$     public string ProductName {get; set;}     Public decimal Amount {get; set;}  74} Copy CodeSpecify validation 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 ("Specify a First name"),  9     rulefor (Customer + customer. Discount). NotEqual (0). When (customer = customer. Hasdiscount)     rulefor (Customer + customer. Address). Length (+),     rulefor (customer = customer. Postcode). Must (Beavalidpostcode). Withmessage ("Please specify a valid postcode");  }13 14   private bool Beavalidpostcode (string postcode )  15   {   //Custom postcode validating logic goes here17  }18}19 20//manual validation rules Cust Omer customer = new Customer (); Customervalidator validator = new CustomeRvalidator (); Validationresult results = validator. Validate (customer); 24 25 bool validationsucceeded = results. isvalid;26 ilist<validationfailure> failures = results.  Errors; How does replication code flent validation integrate with the ASP. NET MVC validation library? If this is true in ASP. NET MVC, there may be a lot of people who don't know about him, and we know that the ASP. NET MVC project has its own certification authority [Enterprise Library VAB (Validation application Block), Based on attribute declarative validation], the use of the method, we have been very recognized, but it is also a lot less flexible, but fluent validation is indeed more flexible. The use of variability, fluency, and validation rules are a separate class, and business entity objects are categorized, we do not need to vab like, need to use attribute registration validation rules on the business entity class.   Since it is not the default validation rule class library for ASP. NET MVC, we need to register it in the validation rules Library of ASP.        Copy Code 1//register as the default validation rule library for ASP. NET MVC in the Applicaton_start () function in Global.asax.cs. 2 3//Fluent validation4 Fluentvalidationmodelvalidatorprovider Provider = new Fluentvalidationmodelvalidatorprovider (New Attributedvalidatorfactory ()); 5 ModelValidatorProviders.Providers.Add (provider); 6 7 dataannotationsmodelvalidatorprovider.addimplicitrequiredattributeforvaluetypes = false; Copy Code Note: 1,) as fluent validation validation rule class must inherit abstractvalidator&Lt t>;  2,) we can also rewrite the validator (type type) function of the Attributevalidatorfactory class in the same way as Nopcommerce, and support other validation rules in a special business environment.

. NET business entity class validation component fluent Validation

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.