SIGN UP MEMBER LOGIN:    
ARTICLE

Introduction to Enterprise Library: Part II

Posted by Sateesh Arveti Articles | Visual C# January 19, 2009
This article explains about Attribute based validation in Validation Application Block.
Reader Level:
Download Files:
 

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:

  1. Store Correct information: By validation, we can make sure the data entered is proper. For example, an account balance should be numeric only.

  2. Security: By validation, we can protect our code from threats, hacking techniques and SQL injection etc.

  3. 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:

  1. Less Code: By using VAB, amount of code needed for validations get reduces.

  2. Code Readability: It's easy to understand the code by using VAB.

  3. Easy to maintain: It's easy to modify code without side-effects.

  4. 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:

  1. using Attributes

  2. using Configuration and

  3. using Code.

VAB comes with a set of adapters for working with following technologies:

  1. ASP.NET

  2. Windows Forms and

  3. 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.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor