Introduction to the Validation Application Block

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

Any application that accepts input either from users or from other systems must ensure that the information is valid in terms of some set of rules that you specify. For example, when processing an order, you may need to check that a customer's phone number has the correct number of digits, or that a date falls within a particular range. In addition, if the validation fails, you may need to 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 for null strings and another validator checks that a number falls within a specified range.

There are also special validators named AndCompositeValidator and OrCompositeValidator. If you create an AndCompositeValidator, which aggregates other validators, all validators in the composite must be true for a successful validation. If you create an OrCompositeValidator, at least one of the validators in the composite must be true for a successful validation.

You can also group validators together in a rule set. A rule set allows you 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.

There are three ways to perform validation within the application block, and to create rule sets. They are:

  • Using configuration
  • Using attributes
  • Using code

In addition, the Validation Application Block includes adapters that allow you to use the application block with the following technologies:

  • ASP.NET
  • Windows Forms
  • Windows Communications Framework (WCF)

Common Scenarios and Goals

Validation has many applications. For example, you can use it to prevent the injection of malicious data by checking to see if a string is too long or if it contains illegal characters. You can also use validation to enforce business rules and to provide responses to user input. It is often important to validate data several times within the same application. For example, you may need to validate data at the UI layer to give immediate feedback when a user enters an invalid data value, and again at the service interface layer for security.

An Initial Example

The Validation Application Block is designed to allow you to easily validate objects. In many situations, you can validate an object with a single line of code. The following example shows how to associate a validator with an object and how to validate that object.

using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
public class Customer
{
    [StringLengthValidator(0, 20)]
    public string CustomerName;

    public Customer(string customerName)
    {
        this.CustomerName = customerName;
    }
}

public class MyExample
{
    public static void Main() 
    {
        Customer myCustomer = new Customer("A name that is too long");
        ValidationResults r = Validation.Validate<Customer>(myCustomer);
        if (!r.IsValid)
        {
            throw new InvalidOperationException("Validation error found.");
        }
    }
}
'Usage
Imports Microsoft.Practices.EnterpriseLibrary.Validation
Imports Microsoft.Practices.EnterpriseLibrary.Validation.Validators
Public Class Customer
    <StringLengthValidator(0, 20)> _
    Public CustomerName As String
    Public Sub Customer(ByVal customerName As String)
        Me.CustomerName = customerName
    End Sub
End Class
Module Module1
    Public Sub Main()
        Dim myCustomer As Customer = New Customer()
        myCustomer.CustomerName = "A name that is too long"
        Dim r As ValidationResults = Validation.Validate(myCustomer)
        If Not r.IsValid Then
            Throw New InvalidOperationException("Validation error found.")
        End If
    End Sub
End Module

Because the StringLengthValidator attribute is applied, the Validation Block checks that the length of the customer name is between 0 and 20 characters. The application creates a new Customer object and validates it using the Validation Block façade. In this case, the customer name is illegal because it is too long. The application throws an exception that notifies you of the error.

Highlights

The Validation Application Block has the following benefits:

  • It helps maintain consistent validation practices.
  • It includes validators for validating most standard .NET data types.
  • It allows you to create validation rules with configuration, with attributes, and with code.
  • It allows you to associate multiple rule sets with the same class and with members of that class.
  • It can be integrated with ASP.NET, Windows Forms, and Windows Communications Foundation (WCF).

System Requirements

To run the Validation Application Block, you need the following:

  • Microsoft Windows XP Professional, Windows Server 2003, or Windows Vista operating system
  • Microsoft .NET Framework 2.0 or 3.0. The Windows Communications Foundation (WCF) adapter requires 3.0.
  • Microsoft Visual Studio 2005 development system (any of the following editions):
    • Microsoft Visual Studio 2005 Standard Edition
    • Microsoft Visual Studio 2005 Professional Edition
    • Microsoft Visual Studio 2005 Team Edition for Software Developers
    • Microsoft Visual Studio 2005 Team Edition for Software Testers
    • Microsoft Visual Studio 2005 Team Edition for Software Architects
    • Microsoft Visual Studio 2005 Team Suite

Source Code and Binaries

The Enterprise Library includes both the source code and the binaries for the Validation Application Block. You can use the binaries if you have no plans to customize the Validation Application Block source code.

If you do plan to customize the source code, you will need to compile a new set of binaries that reflect your changes. For information on the Enterprise Library binaries, see Using the Enterprise Library Binaries

Validation Application Block Dependencies

The Validation Application Block depends on other code included in the Enterprise Library:

  • Core library functionality. The Enterprise Library Core provides services, such as instrumentation and configuration, and is a shared dependency of all Enterprise Library application blocks. The core library functionality is contained in the assembly Microsoft.Practices.EnterpriseLibrary.Common.dll.
  • The ObjectBuilder subsystem. The ObjectBuilder subsystem performs all the repetitive and necessary tasks for creating and disposing object instances, while still providing a high level of flexibility. Enterprise Library uses the ObjectBuilder subsystem for tasks such as injecting configuration into block classes and connecting instrumentation classes to application blocks. The ObjectBuilder subsystem is contained in the assembly Microsoft.Practices.ObjectBuilder.dll.

Validation Application Block Documentation

In addition to the introduction, the documentation contains the following topics:

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.