Introduction :
In Previous article, we had gone through Enterprise Library and its components. In this article, we will start our journey with Validation Application block [VAB]. We will go through series of samples that make use of this block. Validation is quite common activity in any kind of applications. Before looking into this block, first discuss why validation is necessary:
-
Store Correct information: By validation, we can make sure the data entered is proper. For example, an account balance should be numeric only.
-
Security: By validation, we can protect our code from threats, hacking techniques and SQL injection etc.
-
System crash: By validation, we can make sure our code won't get crashed because of invalid data like storing non-numeric data in a numeric variable etc.
Because of above reasons, validation became quite essential task in most of the projects.
Now, let's see the benefits of using VAB are:
-
Less Code: By using VAB, amount of code needed for validations get reduces.
-
Code Readability: It's easy to understand the code by using VAB.
-
Easy to maintain: It's easy to modify code without side-effects.
-
Centralization of code: By using VAB, we can place validation logic in a single place for easy maintenance.
VAB is based on a set of classes called as validators. These validators provide basic validation functionality like null checking, string length checking etc. By combining these built-in validators and using and/or conditions, we can create complex validators.
We can combine validators by using Rule sets. We will look into rule sets deeply in coming articles. For time being, assume rule set combines two or more different validators and make sure that all validations are carried out.
By using VAB, we can create validators in three different ways:
-
using Attributes
-
using Configuration and
-
using Code.
VAB comes with a set of adapters for working with following technologies:
-
ASP.NET
-
Windows Forms and
-
WCF
Let's start with an example showing Attribute based validation implementation. Open VS 2008 having Enterprise Library 4.1 got installed.
Create a new console application and name it as AttrBasedVAB. Add reference to following assemblies present in installation folder of Enterprise Library:
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Validation.dll
And import
Microsoft.Practices.EnterpriseLibrary.Validation.Validators
Microsoft.Practices.EnterpriseLibrary.Validation namespaces.
Create an employee class in program.cs as shown below:

Now, we will add some validations like empid should be less than 2000; empname should not be more than 10 characters by using VAB attributes as shown below:

Here, rangevalidator validates empid between 0-2000 inclusive and stringlength validator validates empname should be less than 10 characters. By default, all validators will be having default error message. MessageTemplate property is used to override default error message and set a custom error message.
Now, go to Main method and add below code:

Here, we are creating an Employee and Validator instance and validating objEmp object using the validator's Validate Method. This method will return list of validation messages. We are looping through this validation results and displaying on console.
Run the application, we can see the validation messages on console as shown below:

In this way, we can implement attribute based validation using VAB. My suggestion is use attribute based validation, if it is a new project starting from scratch. Since, it's hard to implement this validation in already existing code base, because it requires adding this kind of validation in lot of places. Main benefit of attribute based validation is that modifying validation logic is easy, since it is present in a single place.
I experienced the real benefit of this kind of validation in my application. In my project, Job Number should not be more than 7 characters. Current requirement is that it should not more than 9 characters. I seen they are doing this 7 characters validation in lot of places. Finally, I had gone through all modules and modified it to 9 characters. If I had used this attribute validation, than I need to change only in one place.
I am ending the things here. I am attaching source code for reference. In coming articles, we will go deep into this Application block. I hope this article will be helpful for all.