Monday, March 12, 2007

* Enterprise Library : Using Validation Application Block

Validation Application Block (VAB) is a new addition to Enterprise Library 3.0 which is scheduled for release in April, 2007. This walkthrough for beginners on how to configure and use VAB is based on the Feb,2007 CTP of Enterprise Library 3.0.

  • VAB provides a generic and extensible framework for validating data.
  • It can be used at any tier of the application.
  • VAB comes with default adapters to integrate it with Asp.NET, Windows Forms or WCF based applications.
  • The basic building block of VAB are validators and Rule Sets. Validator is a single class responsible for performing a single validation on an object like check for 'null' while rule set is a group of validators to be applied on object.
  • The validations on a type can be configured either in code, through attributes or in app config file. The config file can be modified using the standard enterprise library tool.
  • The main steps in using VAB are as follows :
    • Add VAB assemblies to the references of your project.
    • Add VAB config section to the app config.
    • Start adding the types in this section on which validations need to be performed.
    • For each type add any number of rule sets and configure validators in them.
    • Call Validator methods from application code by passing the object and the rule set against which it needs to get validated.
  • Lets understand all of the above using a sample program.
  • Create a type 'Employee' on which we will perform validation :
using System;
using System.Collections.Generic;
using System.Text;

namespace Employee
{
public class Employee
{
private string name;
private string empNum;

public string Name
{
get
{
return name;
}
set
{
name=value;
}
}
public string EmployeeNumber
{
get
{
return empNum;
}
set
{
empNum=value;
}
}
}
}








  • For this walkthrough we will integrate VAB with a console application. So create a blank console application.

  • Add reference to Microsoft.Practices.EnterpriseLibrary.Validation.dll to the console application project.

  • Next configure the app.config file using the Enterprise Library tool. Add VAB to the file. Add new type to the block by browsing to the Employee type created above by loading the created assembly.

  • After adding the type 'Employee' add a new rule set for the type say 'RuleEmployee'. Make sure that DefaultRule for Employee is RuleEmployee.

  • Right click RuleEmployee -> New -> Choose Members and select property 'Name'.

  • We are going to do validation on Employee instance to check that Name should not be greater than length 20.

  • Under Name property add a new String Length Validator which comes with VAB. Set UpperBound property to 20. Now we are ready to call Validation function from our code.

  • Add the following code to the console application.

  • using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Practices.EnterpriseLibrary.Validation;
    using Employee;

    namespace ValidationAppBlock
    {
    class Program
    {
    static void Main(string[] args)
    {
    Employee.Employee emp = new Employee.Employee();
    emp.Name="Employee's Name";
    emp.EmployeeNumber="11";
    ValidationResults results = Validation.Validate<Employee.Employee>(emp);
    if(results.IsValid)
    {
    Console.WriteLine("success");
    }
    else
    {
    foreach (ValidationResult result in results)
    {
    Console.WriteLine(result.Message);
    }
    }
    }
    }
    }






  • Validation.Validate is the method used to invoke VAB block. results.IsValid equals true means the validation returned success. Change the length of Name to more than 20 and check for the output.




The length of the value must fall within the range "0" (Ignore) - "20" (Inclusive).
Press any key to continue . . .





  • In my next post on this i will try to cover 'How to create and configure a custom validator which is most powerful feature of this block'


~tata & take care~



Other Posts



3 comments:

  1. I have a class in my winforms and want to use that in Validation.

    I am not able to see the class when i click Load assembly.

    Please let me know how to load the class(Ex:Employee class)

    ReplyDelete
  2. Hi Pawan,
    are you using the final April/2007 release of EntLib 3.0 as earlier version has some issue. If you directly mention it in XML it will work.

    vikas

    ReplyDelete
  3. Thx Vikas..I am able to refer now after adding to the config file.

    But,Isn't it strange ? Is it a bug or something else ?

    ReplyDelete