lmost any application you write that accepts input requires some sort of validation to meet the business requirements. One example is when you need to validate user-entered or externally acquired data before persisting it in the database. .NET provides a rich set of features for handling validation; but even so, developers sometimes find themselves writing the same type of validation "plumbing" code repeatedly. To help solve this, Enterprise Library 3.0 ships with a new Validation Application Block that abstracts this plumbing code in the form of a reusable library that you can use in any .NET application. This article introduces the Validation Application Block and provides working examples of how you can use it to write robust validation code.
Introducing the Validation Application Block
The Validation Application Block includes a comprehensive library of common validation rules that apply to primitive data types. These include rules for validating string lengths, date ranges, regular expressions and so on. You can leverage these validators directly from within your code (programmatically or in the form of declarative attributes) or from a configuration file. When the built-in validation does not satisfy your requirements, the Validation Block gives you the option of creating custom validator classes.
After you install Enterprise Library 3.0 April 2007 edition, you'll find the Validation Application Block assemblies in the :\Program Files\Microsoft Enterprise Library 3.0 - April 2007\Bin folder. To use the Validation Application Block, you must reference these three assemblies:
Get Familiar with the Built-in Validators
- Microsoft.Practices.ObjectBuilder.dll assemblies
The Validation Block ships with a number of built-in validator classes, all of which derive from a base Validator class. Table 1 provides a brief overview of the built-in validators.
||Allows you to create a composite validator by combining one or many validators. You can specify either "And" or "Or" type validation through CompositionType enumeration.
||Checks a string for the presence of the characters specified in the CharacterSet property.
||Lets you check whether a supplied DateTime object falls within a specified range.
||Checks whether a value matches one of a list of supplied values.
||Checks whether a string can be converted to an enum.
||Checks to ensure a supplied value is not null.
||Ensures that an object is a collection of a given type, and invokes validation on each element in the collection.
||Validates an object reference.
||Compares the value of a property with another property.
||Checks whether a value falls within a specified range.
||Checks whether a value matches the pattern specified by a regular expression.
||Checks whether a value falls within a specified date/time range.
||Ensures that the length of a string is within specified limits.
||Checks whether a string can be converted to a specific type.
Now that you have seen an overview of the available validators, I'll walk you through a simple example.