Validation Application Block (Enterprise Library 5.0)

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 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 2 - 5 characters in length and an Age property value must be in the range of 1 - 100.
 
Validation-Application-Block-1.jpg
 
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, which can be freely downloaded from the Codeplex site.
 

Why we need to Validate

 
Correctness
  1. Ensure the system doesn't process invalid data
  2. Enforce business rules
  3. Ensure user input is compatible with internal structures, data schemas, and external systems
Responsiveness
  1. Flag invalid data before performing expensive processing tasks
Security
  1. Protect against script injection and other attacks
Procedure for using a Validation Application Block
  1. Create your business objects
  2. Decorate them with validation rules
  3. Validate your data where required
  4. Process or display validation results
Configuring Validation Application Block
  1. Open your Visual Studio project; if an app.config file is not already available, then add it
  2. Open Microsoft Enterprise Library console.
  3. 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
     
  4. 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.
  5. 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
     
  6. Click the "OK" button, and you must observe that the screen will appear as shown in the image below.
     
    Validation-Application-Block-4.jpg
     
  7. Now we have the type loaded that we want to validate and now it's time to add the Rules we wish to apply.
  8. As shown in the image above, click on the small icon and click on the "Add Validation Ruleset".
     
    Validation-Application-Block-5.jpg
     
  9. The screen will now appear as shown in the image below.
     
    Validation-Application-Block-6.jpg
     
  10. Click on the area shown in the image above, which 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
     
  11. 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
     
  12. 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
     
  13. 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
     
  14. Similarly, add validation details for the Age property we have in the class code, and it must appear as:
     
    Validation-Application-Block-11.jpg
     
  15. 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 which 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.
 
Validation-Application-Block-13.jpg
 
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.


Similar Articles