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

using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Microsoft.Practices.EnterpriseLibrary.Validation;

User.CS

public class User

{

    public User(string firstname, string lastname, string city, string state, string zip, string country)

    {

        this.FirstName = firstname;

        this.LastName = lastname;

        this.City = city;

        this.State = state;

        this.ZipCode = zip;

        this.Country = country;

    }

 

    [NotNullValidator()]

    [StringLengthValidator(1, 50)]

    public string FirstName { get; set; }

 

    [NotNullValidator()]

    [StringLengthValidator(1, 50)]

    public string LastName { get; set; }

 

    [NotNullValidator()]

    [StringLengthValidator(1, 50)]

    public string City { get; set; }

 

    [NotNullValidator()]

    [StringLengthValidator(1, 50)]

    public string State { get; set; }

 

    [NotNullValidator()]

    [StringLengthValidator(1, 10)]

    public string ZipCode { get; set; }

 

    [NotNullValidator()]

    [StringLengthValidator(1, 50)]

    public string Country { get; set; }

}

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.
 

private ValidatorFactory factory; 

    protected void btnSubmit_Click(object sender, EventArgs e)

    {

        User objUser = new User(txtFirstName.Text, txtLastName.Text, txtCity.Text, txtState.Text, txtZip.Text, txtCountry.Text);

        Validator<User> validator = ValidationFactory.CreateValidator<User>();

        ValidationResults results = validator.Validate(objUser);

        if (!results.IsValid)

        {

            throw new InvalidOperationException("Validation error found.");

        }

        else

        {

            Label1.Text = "Successfully Validated";

        }

 

    }


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.

[NotNullValidator()]

[StringLengthValidator(1, 10)]

public string ZipCode { get; set; }

img2.jpg

Image 2.

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

img3.jpg

Image 3.

Up Next
    Ebook Download
    View all
    Learn
    View all