
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:
- Microsoft.Practices.EnterpriseLibrary.Common.dll
- Microsoft.Practices.EnterpriseLibrary.Validation.dll
- Microsoft.Practices.ObjectBuilder.dll assemblies
Get Familiar with the Built-in Validators
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.
| Class |
Description |
| ValidatorComposition |
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. |
| ContainsCharactersValidator |
Checks a string for the presence of the characters specified in the CharacterSet property. |
| DateTimeRangeValidator |
Lets you check whether a supplied DateTime object falls within a specified range. |
| DomainValidator |
Checks whether a value matches one of a list of supplied values. |
| EnumConversionValidator |
Checks whether a string can be converted to an enum. |
| NotNullValidator |
Checks to ensure a supplied value is not null. |
| ObjectCollectionValidator |
Ensures that an object is a collection of a given type, and invokes validation on each element in the collection. |
| ObjectValidator |
Validates an object reference. |
| PropertyComparisonValidator |
Compares the value of a property with another property. |
| RangeValidator |
Checks whether a value falls within a specified range. |
| RegexValidator |
Checks whether a value matches the pattern specified by a regular expression. |
| RelativeDateTimeValidator |
Checks whether a value falls within a specified date/time range. |
| StringLengthValidator |
Ensures that the length of a string is within specified limits. |
| TypeConversionValidator |
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.