Introduction
Validation is one of the most important parts in any software project. Building flexible business validation is every one's dream. Rather than writing frameworks from scratch to do these things, Microsoft validation blocks makes it a breeze. In this article we will discuss how validation application blocks help us to build flexible validations using validation application blocks. Its just a simple sixteen step process to put our business validation in action using validation blocks.
The problem
No business can work without validation. Software's are made to make business automated so validation forms the core aspect for any software. Almost 80% projects in .NET implement validation as shown in figure below.

Figure: - Validation implemented
The above figure shows how business validations are implemented for a simple customer class. We have two properties customer code and customer name. Both of these properties are compulsory. So we have created a class called as 'ClsCustomer', with two set/get properties for customer code and customer name. If we find that customer code is empty in the set of the property we raise an error. The same methodology we have used for customer name.
Now we can consume the class in the ASPX UI as shown in figure 'Business object consumed'. We have created the object, set the properties and in case there is error we have caught the same and displayed it in a label using try catch exception blocks.

Figure :- Business object consumed
This implementation looks good to some extent as we have put the business validation in the class and decoupled the UI from any business validation changes.
Let's list down the common problems associated with the above methodology:-
Tangled validation: - If the validation rules changes we need to compile the whole class. As the entity and the validation layer are entangled in one class, changes in validation leads to compilation of the whole class. So out first goal should be decouple this validation logic from the entity class. When we say entity class we are referring to the 'ClsCustomer' class.

Figure :- Tangled validation

Figure :- Decoupling validations from the entity class
No need of compiling: - Validations are volatile entity. They change from time to time. If we can change validation constraints without compiling will lead to better maintenance of the system.
One go validation and results: - The second issue is validations are executed in every set of the property and errors are displayed one by one. It would be great if we can validate all the validation once and display all the errors once.
All these things can be achieved by using Microsoft validation application blocks.
The solution: - validation application blocks
Step 1:- Download the Enterprise library 4.0 from here.
Step 2:- Once you install it you should see the same in programs - Microsoft patterns and practices.
Before we move ahead to understand how validation application blocks works. We will first walkthrough fundamentally how validation application blocks decouples the validation from the entity classes. Validation application blocks stands in between the entity class the validation. Validations are stored in configuration files like web.config or app.config depending whether it's a web application or a windows application. So the validation blocks reads from the configuration file the rules and applies it over the entity class. We can maintain these validations using the enterprise configuration screen provided by Microsoft blocks.

Figure: - Decoupling validations from entity classes
Now that we have understood how validation blocks will work. We will run through a example practically to understand the concept.
Step 3:- Once you have installed the block, click on Enterprise library configuration UI to define validation on the configuration file
Figure :- Loading Enterprise configuration screen
Step 4:- Click on open and browse to your web.config file and click the open button.
Figure :- Open the web.config file
Step 5:- Once you have opened the web.config file you should see a tree like structure as shown in figure 'Add validation block'. Right click on the main tree and add the validation application block to your web.config file.

Figure :- Add validation application block

















JohneditedPosted Jul 13, 2010, 8:42 AMEdited Jul 13, 2010, 8:43 AM
I have read another article about validation with xml definition. There you need set required object/property class into xml and run Validate when you need. You can find more info on it here .Net validation
mostafa hassanPosted Aug 29, 2009, 3:26 PM
Nice Work
Vishal DograPosted Aug 27, 2009, 6:40 AM
Hi Shiv, Thanks for submitting such a informative article which is short and precise and hence easier to understand. Two limitations, however, occur to me here: 1) Performance: Since validation run on the object and not on the UI elements, think of complex screen which may contain lots of UI elements. In this case we will have to first copy the Ui values to object so that validation can run on it. This may impact the performance. 2) Security: Since the validation rules get copied to .config file in plain text this exposes the business rules and also make them alterable by people with not good intentions. Just want to have your opinion on this and any solutions you mar recommend to overcome them. Vishal