C#: Microsoft Enterprise Library: Validation

Category: .NET Framework

C#: Microsoft Enterprise Library: Validation

By: Zack Turnbow

Introduction

In this next article covering Microsoft’s Enterprise Library, the Validation Application Block (VAB), will be introduced. The VAB is not one of the original application blocks when the Enterprise Library first came out. Its first appearance came in version 3.0. The VAB does exactly as the name implies, it validates data. The issue of validating data can be an extensible, especially if there is a need for custom validation. Some of the functionality of the VAB is: utilized at any tier of an application and there are default adapters that can assist in integrating with existing .NET applications.

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

Implementation

Before any code or configuring is done, it is good to explain the basics first. In the VAB there are two entities that are used. One being a validator and the other being a rule set. The validator is a solitary class that is in charge of carrying out a single validation on set or object of data. The specific validation is done either in code or it can be configured in an app.config file.

If you have been following the previous articles, then the same Visual Studio project will be used. So, as with the other articles, a reference to the VAB needs to be added to the project. Go ahead and create the reference.


[click to see full-size]

Add a new windows class library to the solution and call it Product. Then in the new class add the following code:

using System;
using System.Collections.Generic;
using System.Text;

///


/// Summary description for Product
///

namespace Product
{
public class Product
{
private string productName;
private string productNumber;

public string ProductName
{
get
{
return productName;
}
set
{
productName = value;
}
}

public string ProductNumber
{
get
{
return productNumber;
}
set
{
productNumber = value;
}
}
}
}

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

Build the project and then open up the Enterprise Library Configuration Application and open up the web.config to the project in Visual Studio.


[Click to see full-size]

In the left pane, right click on the web.config and select New – Validation Application Block. From there right click the new VAB and select New – Type. A new window will open up to show all of the assemblies.


[Click to see full-size]

Click the Load an Assembly button and navigate to the Obj folder to the class library project and select the Product.dll file. Then scroll to the where the new class is found. Click the Product class and then click OK


[Click to see full-size]

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

Right click the Product type and select New – Rule Set and call it ProductRule. Go back to the Product type and make sure that the DefaultRule is set to the new rule that was just created. When that is completed, right click on the ProductRule and select New – Choose Members. A new window will open with the layout of the Product class. Check the box for the ProductName property.


[Click to see full-size]

Now right click the ProductName member in the left pane and select New – String Length Validator. In the right pane set the UpperBound to 10. In most cases this will usually be set to what the database maximum is but for this article 10 will be used.


[Click to see full-size]

Save and then close the Enterprise Library Configuration application. Go ahead and go back to Visual Studio and open up the Default.aspx page. Drag a label from the toolbox into the work area. If the previous article’s project is being used, comment out the Caching code and then add references to the Enterprise Library Validation block and the Product project and then open up the code behind and add the two using statements:

using Microsoft.Practices.EnterpriseLibrary.Validation;
using Product;

Enter the following code under the Page_Load event:

Product.Product pro = new Product.Product();

pro.ProductName = "Product Number Limit";
pro.ProductNumber = "11";

ValidationResults res = Validation.Validate(pro);

if (res.IsValid)
{
Label3.Text = "Product Name Validated Successfully";
}
else
{
foreach (ValidationResult result in res)
{
Label3.Text = "Product Name Exceeds Upper Bound Limit";
}
}

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

Go ahead and run the application to see the results.


[Click to see full-size]

Go back and change the ProductName in the code behind to: Product1 and then re-run the application and compare the results.


[Click to see full-size]

From this article, it has been shown how easy it is to institute a validation policy and giving the application the ability of changing the policy without having to change any code.

What have we learned?

How to integrate the Enterprise Library Validation Application Block into an existing application.
How to configure the VAB and setup validation rules and validation types.
How to implement the VAB in code.

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

Attachments



Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!