Introduction to Enterprise Library: Part IV

In previous article, we had discussed about attribute based validators and rule sets. Now, we look into validation through config files. We will discuss about Enterprise Library Configuration tool followed by an example. Create a new Console Application in VS 2008 and name it as ConfigBasedVABClient.

Add new properties to Employee class and add it to a new class Library ConfigBasedVABServer as shown below:

Build the application. Go to Enterprise Library Configuration tool present in installation folder or Start  Programs  Microsoft patterns & practices  Enterprise Library 4.1 - October 2008.

Click on New Application and select Validation Application Block as shown below:

 

  1. Select New  Type  Load From File  Select ConfigBasedVABServer dll path  Select Employee  Ok.
  2. Select New Rule Set  Name as Group1 
  3. Right Click Group1  New  Choose Field  Check EmpID.
  4. Right Click EmpID  New Range Validator  Set required properties as shown below:


     
  5. Now save it as ConfigurationVAB.

Similarly, we can add other validators on Employee properties.

Go to ConfigBasedVABClient and add reference to ConfigBasedVABServer and required VAB dlls discussed in previous article.

Add app.config to the client project and paste the contents of ConfigurationVAB.config into it as shown below:

Add the below code to Main method:

Run the application, the output will be like this:

In this way, we can add our validators to existing assemblies using config files without the need of making changes to existing code.

Benefits of using Config based Validators:

  1. No need of source code for implementing the validation.
  2. No need to recompile after making changes to validation logic in config files.
  3. No need to change existing code.

I am ending up the Config based validation here. In coming articles, we will go deep into this Application block. I hope this article will be helpful for all.