What is Validation
Validation is a process that ensures the sanity of data entered, received or processed by a software module, class or class members.
Some examples of validation are length check, range check, Null check, date range, specific character and so on.
Consider you have a class with some properties or methods in it that requires a validation for data handled by these methods or properties. For example, in the code shown below assume that you need to validate that the Name property has a name value with a 2 - 5 characters in length and an Age property value must be in the range of 1 - 100.
In order to validate these two properties we must write validation code in the class itself so before it sets the data for the object, the data is validated as per business rules.
To make the developer community more productive and simply de-couple the validation functionality from the business logic or application code, a Validation Application block can be used.
In order to use a Validation Application block, you must have Enterprise Library 5.0 installed, that can be freely downloaded from the Codeplex site.
Why we need to Validate
Correctness
- Ensure the system doesn't process invalid data
- Enforce business rules
- Ensure user input is compatible with internal structures, data schemas and external systems
Responsiveness
- Flag invalid data before performing expensive processing tasks
Security
- Protect against script injection and other attacks
Procedure for using a Validation Application Block
- Create your business objects
- Decorate them with validation rules
- Validate your data where required
- Process or display validation results
Configuring Validation Application Block
- Open your Visual Studio project; if an app.config file is not already available, then add it
- Open Microsoft Enterprise Library console.
- In the Blocks menu, choose the "Add Validation Settings" option and your screen will appear, as shown in the image below:
![Validation-Application-Block-2.jpg]()
- From the loaded screen, click on the "+" button, to add a type we want to validate, this will allow you to add a type, either from the GAC or from a file. We will use a file for our demo purposes here.
- Once a type is selected, it will load the DLL and show you the class inside it, navigate through and pick the class that you want to validate. The screen will appear as shown in the image below.
![Validation-Application-Block-3.jpg]()
- Click the "OK" button, and you must observe that the screen will appear as shown in the image below.
![Validation-Application-Block-4.jpg]()
- Now we have the type loaded that we want to validate and now it's time to add the Rules we wish to apply.
- As shown in the image above, click on the small icon and click on the "Add Validation Ruleset".
![Validation-Application-Block-5.jpg]()
- The screen will now appear as shown in the image below.
![Validation-Application-Block-6.jpg]()
- Click on the area shown in the image above, that will open the popup, and from there we will choose "Add Property to Validate" as shown in the image below.
![Validation-Application-Block-7.jpg]()
- In the opened window, add the name of the property you wish to validate, as shown in the image below.
![Validation-Application-Block-8.jpg]()
- Now look for the same icon on the top-right corner and it will prompt you for further actions to pick type validation etc. as shown in the image below.
![Validation-Application-Block-9.jpg]()
- Now, click on "Add String Length Validator" and enter the basic required setting for the Validation rule, as shown in the image below.
![Validation-Application-Block-10.jpg]()
- Similarly add validation details for the Age property we have in the class code, and it must appear as:
![Validation-Application-Block-11.jpg]()
- Save this from "File" -> "Save" menu to a .configfile. If you already have one config file in your application then browse to that location when saving, otherwise you can create a new one and add that to your project. After properly performing Steps 1 through 15 that are quite intuitive, you will have a config file as shown below.
![Validation-Application-Block-12.jpg]()
Consuming Validation Application Block with Client Application
After adding the .config into the client application, you only need to write some code to tie the validation logic in the .config with the calls happening via the object to the class properties.
This code will ensure that Name is at-least 2-5 characters long and Age is between 1 and 100.
Test run the application and enter data as I have shown in the image below, that will Pass the validation.
![Validation-Application-Block-14.jpg]()
But if you try entering data that doesn't pass through the validation then the error message will be displayed that we configured in the config file.
![Validation-Application-Block-15.jpg]()
Conclusion
Validation is a mandate for all kinds of applications, and a Validation Application Block is one of the finest ways of implementing it without much coding efforts required in the business class.