Huang Cong: Microsoft Enterprise Library 5.0 series tutorial (3) Validation Application Block (advanced)

Source: Internet
Author: User

Configuration File mode for Enterprise Library Verification Application Module:

 

1. Create a console application and create a customer class. The Code is as follows:

Code

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Using Microsoft. Practices. enterpriselibrary. validation. validators;
Using Microsoft. Practices. enterpriselibrary. validation;

Namespace consoleapplication1
{
Class Program
{
Staticvoid main (string [] ARGs)
{
}

Publicclass customer
{
Publicstring customername;

Public customer (string customername)
{
This. customername = customername;
}

Publicint test ()
{
Return-5;
}
}
}

2. Run entlibconfig.exe and selectBlocksMenu, clickAdd validation settings.

 

3. ClickValidated typesClick the plus sign in the upper-right corner of the block, and then clickAdd type to validateIn this case, you need to select the class to be verified, because we want to verify the attributes of the customer class, so we need to import the class we just created, click andAdd from FileFind the leleapplication1.exe under the debug folder of our application program (which varies with the name you created). The customer class is included in this program:

 

4.
After the import, we can see that there is an consoleapplication1 assembly, and then we want to select the customer class it contains, as shown in, and click OK:

 

 

5.
On the customer panel, right-click-add validation ruleset:

 

6.
In the defaule ruleset attribute on the set customer panel, right-click the newly created validation ruleset attribute on the validation ruleset panel-select members, and select

 

 

7.
On the pop-up member selection page, select the test method and customername attribute to be tested:

 

 

8.
Right-click the field: mermername panel and choose "add validators-add not ".
Null validator:

 

 

9.
Shows the settings for the validator:


 

10. Right-click the method: Test Panel and choose add validators-add range.
Validator:

 

 

11. The settings for this validator are shown in:

 

 

12. ClickFileMenu, clickSave, Save asApp. configFile, you can save it to the desktop first, and then use it.

13. Add app. config to the project and add the required references ,:

 

14. test:

Code

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Using Microsoft. Practices. enterpriselibrary. validation. validators;
Using Microsoft. Practices. enterpriselibrary. validation;

Namespace consoleapplication1
{
Class Program
{
Staticvoid main (string [] ARGs)
{
Validator <customer> customervalidator = validationfactory. createvalidator <customer> ("validation ruleset ");

Customer mycustomer = new customer (null );

Validationresults r = customervalidator. Validate (mycustomer );

If (! R. isvalid)
{
For (INT I = 0; I <R. Count; I ++)
{
Console. writeline ("error {0}:" + R. elementat (I). Message, I + 1 );
}
}
Else
{
Console. writeline ("correct ");
}
}
}

Publicclass customer
{
Publicstring customername;

Public customer (string customername)
{
This. customername = customername;
}

Publicint test ()
{
Return-5;
}
}
}

15. Running result:

 

Demo Download \ ( ̄ []  ̄ \)

 

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.