SIGN UP MEMBER LOGIN:    
ARTICLE

Introduction to Enterprise Library: Part V

Posted by Sateesh Arveti Articles | Coding Best Practices February 05, 2009
This article explains about implementation of Self Validation using Validation Application Block.
Reader Level:
Download Files:
 

In previous article, we had discussed about implementation of validators using Enterprise Library's Configuration Tool. In this article, we will look into validator's implementation using custom Code and end up with Message templates. Sometimes, the built-in validators might not serve our purpose. In those cases, we can create our own validating methods.

Open your Visual Studio 2008 and create a new console application, name it as CodeBasedVAB. Add required VAB dlls, which was specified in previous articles. Now, Go to program.cs and add this below class definition:

Validation through custom code is called as self validation. We need to decorate the class having self validation with [HasSelfValidation]. We had defined validation methods for checking negative salary and valid emp Ids with a Self Validation attribute as shown above.

Now, we will validate this Employee object in Main method as shown below:

Run the application, the output will be like this:

 

Now, we look into MessageTemplate attribute. All the validators discussed so far are having a MessageTemplate attribute. This attribute is used to set the validation message to be displayed on fail of its validation. By default, all validators will be having an in-built Message template. Like, default MessageTemplate for Range validator is “The length of the value must fall within the range "0" (Ignore) - "2" (Inclusive).”

We will see how to pass substitution values to MessageTemplate. Add this code as shown below:

Now, run the application. The output will be like this:

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
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor