This article from http://www.cnblogs.com/virusswb/archive/2010/06/19/Enterprise-Library-5-0-Validation-1.html
Overview
Any application that accepts input from users or other systems must ensure that the information is legal and complies with specific rules. For example, when processing an order, you must check that the customer's phone number must be a number. In addition, if verification fails, an error message is returned.
The enterprise database verification module allows developers to implement structured and easy-to-maintain verification solutions. In addition, the verification module can be used in the following technologies:
- ASP. NET
- Windows Communication Foundation (WCF)
- Windwos Presentation Foundation (WPF)
- Windows Forms
The validation module of the Enterprise Library is used to verify the object, not the control used to verify the UI. The object can be verified anywhere.
You can define verification rules in the following way and perform verification:
- Define a rule set for the specified class in the configuration file.
- Add attribute to the object member.
- InCode.
- Use the code to create a validators instance and then run the verification command.
Instance code
Code
Using Microsoft. Practices. enterpriselibrary. validation;
Using Microsoft. Practices. enterpriselibrary. validation. validators;
Public Class Customer
{
[Stringlengthvalidator ( 0 , 20 )]
Public String Customername;
Public Customer ( String Customername)
{
This . Customername = Customername;
}
}
Public ClassMyexample
{
PrivateValidatorfactory factory;
PublicMyexample (validatorfactory valfactory)
{
Factory=Valfactory;
}
Public Void Mymethod ()
{
Customer mycustomer = New Customer ( " A name that is too long " );
Validator < Customer > Customervalidator
= Factory. createvalidator < Customer > ();
// Validate the instance to obtain a collection of validation errors.
Validationresults R = Customervalidator. Validate (mycustomer );
If ( ! R. isvalid)
{
Throw New Invalidoperationexception ( " Validation Error found. " );
}
}
}
Benefits of using the verification module
- Maintain consistent verification experience
- Most of the. NET standard types can be verified.
- Allows you to create verification rules through configuration, attribute, and code.
- The same class can be associated with multiple validation rules.
- When verifying an object, you can apply one or more set of verification rules.
- It can be integrated with ASP. NET, WCF, WPF, and Windows Forms.
AddProgramSet Reference
AddMicrosoft. Practices. enterpriselibrary. validation. dll.
Add
Microsoft. Practices. enterpriselibrary. Common. dll
Microsoft. Practices. servicelocation. dll
Microsoft. Practices. Unity. dll
Microsoft. Practices. Unity. Interception. dll
- Microsoft. Practices. enterpriselibrary. validation. Integration. winforms. dll
- Microsoft. Practices. enterpriselibrary. validation. Integration. ASPnet. dll
- Microsoft. Practices. enterpriselibrary. validation. Integration. WPF. dll
- Microsoft. Practices. enterpriselibrary. validation. Integration. WCF. dll UsingMicrosoft. Practices. enterpriselibrary. validation;
UsingMicrosoft. Practices. enterpriselibrary. validation. validators;
Use of verification module
The verification module contains a class named validators, inherited from the validator class. There is also a generic version of validator <t>.
Each verification is associated with a specific type. For example, the stringlengthvalidator class is used to check whether the string is within the predefined length range.
There are four ways to associate a verification class with a specific type:
- Configuration in the configuration file
- Use attribute
- Use both the configuration file and attribute
- Use self-verification to include the verification logic in the object to be verified
Verification class
Class Name:Andcompositevalidator
Attribute name:Validatorcompositionattribute
Configuration Tool Name:And composite validator
- Contains characters validator
Class Name:Containscharactersvalidator
Attribute name:Containscharactersvalidatorattribute
Configuration Tool Name:Contains characters validator
-
- Date time range validator
-
-
- Domain validator
-
-
- Enum conversion validator
-
-
- Not null validator
-
-
- Object collection validator
-
-
- Object validator
-
-
- Or composite validator
-
-
- Property Comparison validator
-
-
- Range validator
-
- Regular Expression validator
-
-
- Relative date time validator
-
-
- String Length validator
-
-
- Type conversion validator
-
-
- Single Member validators