Microsoft Enterprise Library 5.0 series (iii) 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:

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
{
static void Main(string[] args)
{
}

public class Customer
{
public string CustomerName;

public Customer(string customerName)
{
this.CustomerName = customerName;
}

public int 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 the leleapplication1 assembly is added, and then we want to select the Customer class it contains, as shown in, and then click OK:

5. Right-click the Customer panel and choose Add Validation Ruleset:

6. Set the Defaule Ruleset attribute in the Customer panel to the newly created Validation Ruleset. Right-click the Validation Ruleset panel and choose Select Members from the displayed selection box.

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. the settings for this validator are shown in:

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:

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
{
Static void 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 ");
}
}
}

Public class Customer
{
Public string CustomerName;

Public Customer (string customerName)
{
This. CustomerName = customerName;
}

Public int 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.