. NET Validation component Fluent Validation User Guide, fluentvalidation
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
Introduction to the open-source Codeplex homepage: this component is 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:
Whether it is easy to use or not depends on 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 codeThe Code is 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 {get; set ;}
Public IList <Order> Orders {get; set ;}
Public string Email {get; set ;}
Public decimal Discount {get; set ;}
Public double Age {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 {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 {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 ;}
}
Specific authentication rules for Person:
Copy codeThe Code is 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 first 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
}
}
// Manually verify the rule
Customer customer = new Customer ();
CustomerValidator validator = new CustomerValidator ();
ValidationResult results = validator. Validate (customer );
Bool validationSucceeded = results. IsValid;
IList <ValidationFailure> failures = results. Errors;
How does Flent validation integrate 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 codeThe Code is as follows:
// Register asp.net mvc as the default validation rule repository in the Applicaton_Start () function of Global. asax. cs.
// Fluent validation
FluentValidationModelValidatorProvider provider = new FluentValidationModelValidatorProvider (new AttributedValidatorFactory ());
ModelValidatorProviders. Providers. Add (provider );
DataAnnotationsModelValidatorProvider. AddImplicitRequiredAttributeForValueTypes = false;
Note:
1,) as the Fluent Validation rule class, it must inherit AbstractValidator <T>;
2) We can also rewrite the Validator (Type) function of the AttributeValidatorFactory class following the NopCommerce processing method, and support other verification rules in special business environments.
This article is suitable for users who have some knowledge about. net and MVC. Here, we will introduce you to the end.