Enterprise Library 5 - Validation Application Block

In this article you will learn how to use the Enterprise Library Validation Application Block 5.0 in ASP.Net.

Why to use Validation Application Block

The Enterprise Library Validation Application Block provides some important features that allow developers to implement structured and easy-to-maintain validation scenarios in their applications. In addition, the Validation Application Block includes adapters that allow you to use the application block with the following technologies:

  • Windows® Communication Foundation (WCF)
  • Windows Forms
  • ASP.NET
  • Windows Presentation Foundation (WPF)

There are 3 types of validators in a validation block:

  • Value validators
  • Composite validators
  • Object validators

Value validators

Value validators perform specific validations such as validating the length of a string, checking if the value is null or not, and validating the number against a predefined range.

Composite validators

Composite validators combine with other validators to create a complex set of validations. Two types of composite validations exist: AND validator and OR validator. By using these composite validators with other validators, it is possible to define the validation logic between the validators.

Object validators

Object validators perform all validations defined for the type and supports the validation against the objects in collections.

How to install?

There are two ways to install Validation Application Blocks in an application.

To install an Enterprise Library Exception Handling Application Block, run the following command in the Package Manager Console:

PM> Install-Package Install-Package EnterpriseLibrary.Validation

Or add a reference and find the validation assembly and click "Add".

Getting Started

Begin using the following procedure:

  • Start Visual Studio
  • Create a new website
  • Provide the name and location of website
  • Click "Next"

First of all add two namespaces:

Now create a new class

  1. using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;  
  2. using Microsoft.Practices.EnterpriseLibrary.Validation;  
User.CS

  1. public class User  
  2. {  
  3.     public User(string firstname, string lastname, string city, string state, string zip, string country)  
  4.     {  
  5.         this.FirstName = firstname;  
  6.         this.LastName = lastname;  
  7.         this.City = city;  
  8.         this.State = state;  
  9.         this.ZipCode = zip;  
  10.         this.Country = country;  
  11.     }  
  12.     [NotNullValidator()]  
  13.     [StringLengthValidator(1, 50)]  
  14.     public string FirstName { getset; }  
  15.     [NotNullValidator()]  
  16.     [StringLengthValidator(1, 50)]  
  17.     public string LastName { getset; }  
  18.     [NotNullValidator()]  
  19.     [StringLengthValidator(1, 50)]  
  20.     public string City { getset; }  
  21.     [NotNullValidator()]  
  22.     [StringLengthValidator(1, 50)]  
  23.     public string State { getset; }  
  24.     [NotNullValidator()]  
  25.     [StringLengthValidator(1, 10)]  
  26.     public string ZipCode { getset; }  
  27.     [NotNullValidator()]  
  28.     [StringLengthValidator(1, 50)]  
  29.     public string Country { getset; }  
  30. } 

Now add a few textboxes on the page and put a button and a button click handler for validation.

The following code creates a new User object and validates it using the Validation Application Block facade. Because the string length validator attribute is applied, the block checks that the length of the fields are not null.

  1. private ValidatorFactory factory;   
  2. protected void btnSubmit_Click(object sender, EventArgs e)  
  3. {  
  4.     User objUser = new User(txtFirstName.Text, txtLastName.Text, txtCity.Text, txtState.Text, txtZip.Text, txtCountry.Text);  
  5.     Validator<User> validator = ValidationFactory.CreateValidator<User>();  
  6.     ValidationResults results = validator.Validate(objUser);  
  7.     if (!results.IsValid)  
  8.     {  
  9.         throw new InvalidOperationException("Validation error found.");  
  10.     }  
  11.     else  
  12.     {  
  13.         Label1.Text = "Successfully Validated";  
  14.     }  
  15. } 

Now run the application to see the result.

img1.jpg

Image 1.

As you can see in the figure, Zip is empty so it's not validated because we have given NotNullValidator.

  1. [NotNullValidator()]  
  2. [StringLengthValidator(1, 10)]  
  3. public string ZipCode { getset; }

img2.jpg
Image 2.

It is still not validated because the zip length is more than 10 characters.

img3.jpg
Image 3.