| Introduction to the Validation Application Block |
| Written by n_ahid |
| Friday, 30 January 2009 16:00 |
|
Validation application block has been added to enterprise library 2007 CTP release.
You can download it from here
IntroductionAny application that takes input from user or other system must ensure that the information is correct format, in terms of some rules that specify by several ways.Like a registration form has lots field like social security number, credit card, email has some specific format demand validation check before inserting to database. In addition, if the validation fails, usually send an error message that explains what is wrong. The Enterprise Library Validation Application Block provides a library of classes, called validators, that supplies the code for validating .NET Framework data types. For example, one validator checks that strings are not null and another validator ensures that a number falls within a specified range. You can use a Boolean AND or OR to create composite validators. You can also create groups of validators. A group of validators is called a rule set, although a rule set may contain as few as a single validator. A rule set is a way to validate a complex object or graph by composing different validators of different types and applying them to elements in the object graph. Examples of these elements include fields, properties, and nested objects. The Validation Application Block includes adapters that allow you to use the application block with the following technologies: • ASP.NET • Windows Forms • AJAX • Windows Communications Framework (WCF) • Windows Presentation Framework (WPF) To get more detail information check Validation Application Block Documentation comes with this release. How to use Validation Application Block in your application.Open application configuration file app.config or eb.config by Enterprise Library Configuration tools. Follow below steps to add the Validation Application Block 1. Start the Enterprise Library Configuration Console. To use the configuration console, click Start, point to All Programs, point to Microsoft patterns & practices, point to Enterprise Library 3.0 – January 2007 CTP, and then click Enterprise Library Configuration. 2. Click the Open Existing Application icon and located your config file.. Open application configuration file app.config or eb.config by Enterprise Library Configuration tools. 3. Right-click Application Configuration, point to New, and then click Validation Application Block.
4. Double-click on the type you want to validate.
16. (Optional) Enter the name of the field, method or property in the right pane.
To apply validators to type members
20. Right-click on Validation Application Block, point to New and click Type.
Defining Composite Validators
Now have a look at some simple code
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
namespace ValidationApplicationBlock
{
public class StringValidator
{
/// <summary />
/// Constructor
/// </summary />
public StringValidator()
{
}
/// <summary />
/// Validation
/// </summary />
[NotNullValidator]
[StringLengthValidator(1, 50)]
public string Name;
}
}
StringValidator oStringValidator = new StringValidator();
oStringValidator.Name = txtName.Text; //get the value from use
ValidationResults r = Validation.Validate(oStringValidator);
if (!r.IsValid)
{
errorProvider1.SetError(txtEmail, "Input Not Vail"); //showing error description using error provider
}
Realy cool and simple. Using the Validation Façade In most situations, you can take advantage of the Validation façade. This class allows you to create a Validatorinstance and validate an object with a single line of code. This is shown in the following code example. Customer myCustomer = new Customer( /* ... */ ); ValidationResults r = Validation.Validate(myCustomer); This code is equivalent to the following code example. Customer myCustomer = new Customer( /* ... */ ); Validator<customer /> v = ValidationFactory.CreateValidator<customer />(); ValidationResults r = v.Validate(myCustomer);
You will find more details about Façade pattern here Using Self Validation [HasSelfValidation] public class MaXMin { private int min; private int max; // ... [SelfValidation] public void Check (ValidationResults results) { if (max < min) results.AddResult(new ValidationResult("Max less than min", this, null, null, null)); } }Ruleset Some time need to only care on some specific property then create a Ruleset, that distinguishes the null rules separate from the others farther can checked by using Validation.Validate(subscriber, "UserInfo");
public class UserInfo
{
private string name;
[NotNullValidator(Ruleset="UserInfo")]
[StringLengthValidator(1,200)]
public string Name
{
get { return name; }
set { name = value; }
}
private string emailAddress;
[NotNullValidator(Ruleset="UserInfo")]
[EmailAddressValidator]
public string EmailAddress
{
get { return emailAddress; }
set { emailAddress = value; }
}
public UserInfo () {}
}
ConclusionI hope this gives some idea what are coming with in this block. It"s not fully decided how Microsoft is going to design this block in case off ajax and WCF, I found in some blogs about this issue. Let"s see and wait how it will come…hope this block will also widely used like data access application block.
Related blogs about validation application block here Something wrong with this article? Report it
Set as favorite
Bookmark
Email This
Hits: 123 0 Comments
Write comment
You must be logged in to a comment. Please register if you do not have an account yet.
|
| Last Updated on Friday, 06 February 2009 16:00 |
Latest Articles
- AntiHisto
- Thumbnail images in PHP
- Online Dating Websites
- Hack to enforce the cache of an XmlDataSource to invalidate
- AutoSearch SELECT tag
- Nine ASP.NET Site Navigation Problem Solutions: Part 1
- Enhanced rich edit control
- CheckListBox based on ListBox that supports ReadOnly
- Introduction to COM - What It Is and How to Use It.
- Introduction to the Validation Application Block


