. NET validation component fluent Validation Use guide _ Practical Tips

Source: Internet
Author: User
Tags nopcommerce

Know fluent vaidation.

It's a genius idea to see how simple it is to use this build in a Nopcommerce project, separating data validation from business entity classes, and then knowing this is an open source lightweight validation build.

Fluent Validation translation: fluent verification

Open Source CodePlex Its homepage introduction: This component is a lightweight. NET class library that uses fluent interface definitions and lambda expressions to build validation rules for a business class (a small validation library for. NET that uses a Fluent interface and lambda expression for building validation the rules for you business objects.)

This class library can be used not only in asp.net MVC projects, but also in common class libraries, and certainly in the ASP.net form project.

How to use:  
Whether it is good to use, but also to see if it is really like the official website suggested description. I prefer the example of its official online, one can see the feeling of usage, is absolutely as its name, fluent, this is also an interpretation of the common use of language, infinite for a type to support an infinite number of attribute extensions.

Business entity classes:

Copy Code code as follows:

public class Person
{
public string NameField;
public int Id {get; set;}
public string Surname {get; set;}
public string forename {get; set;}
Public list<person> Children {get; set;}
Public string[] Nicknames {get; set;}
Public DateTime dateOfBirth {get; set;}
public int? Nullableint {get; set;}
Public person ()
{
Children = new list<person> ();
Orders = new list<order> ();
}
public int calculatesalary ()
{
return 20;
}
Public address address {get; set;}
Public ilist<order> Orders {get; set;}
public string Email {get; set;}
Public decimal Discount {get; set;}
Public double as {get; set;}
public int Anotherint {get; set;}
public string CreditCard {get; set;}
public int? Othernullableint {get; set;}
}
public interface Iaddress
{
String Line1 {get; set;}
String Line2 {get; set;}
String Town {get; set;}
String County {get; set;}
String postcode {get; set;}
Country Country {get; set;}
}
public class Address:iaddress
{
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;}
}
public class Country
{
public string Name {get; set;}
}
public interface Iorder
{
Decimal Amount {get;}
}
public class Order:iorder
{
public string ProductName {get; set;}
Public decimal Amount {get; set;}
}

Specify the validation rule for person:  

Copy Code code as follows:

Using Fluentvalidation;
public class Customervalidator:abstractvalidator<customer>
{
Public Customervalidator ()
{
Rulefor (Customer => customer. Surname). Notempty ();
Rulefor (Customer => customer. forename). Notempty (). Withmessage ("Please specify a name");
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
}
}
Manual validation rules
Customer customer = new Customer ();
Customervalidator validator = new Customervalidator ();
Validationresult results = validator. Validate (customer);
BOOL validationsucceeded = results. IsValid;
ilist<validationfailure> failures = results. Errors;

How does the Flent validation integrate with asp.net MVC validation library?
If that's true in asp.net mvc, there's probably 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 verification], the use of the method, we have been very recognized, but there are a lot of inflexible, but fluent validation really more flexible. The use of variability, fluency, and validation rules is a separate class, and business entity objects are categorized, we do not need to Xiang VAB, like the need to use attribute registration validation rules on business entity classes.

Since it is not the default validation rule class library for ASP.net mvc, we need to register to the validation rules library for asp.net mvc.

Copy Code code as follows:

Registered as the default validation rule library for ASP.net mvc in the Applicaton_start () function in Global.asax.cs.
Fluent validation
Fluentvalidationmodelvalidatorprovider Provider = new Fluentvalidationmodelvalidatorprovider (new Attributedvalidatorfactory ());
MODELVALIDATORPROVIDERS.PROVIDERS.ADD (provider);
Dataannotationsmodelvalidatorprovider.addimplicitrequiredattributeforvaluetypes = false;

Attention:
1,) as fluent validation validation rule class must inherit abstractvalidator<t>;

2) We can also override the validator (type type) function of the Attributevalidatorfactory class and support other validation rules in a particular business environment, modelled on the nopcommerce approach.

This article is suitable for readers who have knowledge of. NET and MVC, and here's a shortcoming

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.