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.

This topic includes a series of brief sections that provide information to help you decide whether the Validation Application Block is suitable for your requirements. This topic includes the following sections:

  • Common Scenarios
  • Example Application Code
  • Highlights of the Validation Application Block
  • Determining When to Use the Validation Application Block
  • Alternatives to Using the Validation Application Block

In addition to this introductory material, the documentation contains the following topics:

  • Developing Applications Using the Validation Application Block. This topic explains how to include the Validation Application Block in your applications and how to configure it. It also contains more detailed information, such as how to create custom message templates and information on how validation works with inheritance.
  • Key Scenarios. This topic shows different ways to use the Validation Application Block in your own applications.
  • Design of the Validation Application Block. This topic includes a class diagram of the Validation Application Block.
  • Extending and Modifying the Validation Application Block. This topic explains how to extend the application block by adding custom validators and attributes. It also contains advice about how to modify the source code.
  • Deployment and Operations. This topic explains how to deploy and update the application block assemblies. It also explains the application block's instrumentation.
  • Validation QuickStarts. This topic explains how to install and configure the two QuickStart applications and contains a series of walkthroughs that demonstrate how to incorporate common validation operations into an application.

For details of the system requirements for the Validation Application Block, see System Requirements. For details of the dependencies for the Validation Application Block, see Application Block Dependencies.

Common Scenarios

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.

The Validation Application Block is designed to address the most common tasks developers face when they must validate input either from a user or another system. These tasks are arranged according to scenarios. Each scenario gives an example of a typical way to use the Validation Application Block and shows the code that accomplishes the task.

The scenarios are the following:

Example Application Code

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 then 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(Of Customer)(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 Application 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 Application 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 of the Validation Application Block

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, attributes, and code.
  • It allows you to associate multiple rule sets with the same class and with members of that class.
  • It allows you to apply one or more rule sets when you validate an object.
  • It can be integrated with ASP.NET, Windows Forms, and Windows Communications Foundation (WCF).

Determining When to Use the Validation Application Block

The Validation Application Block allows you to encapsulate validation best practices into easily maintainable code that you can reuse. Encapsulation also allows you to separate the application code from the validation logic. In some situations, you may be able to update the validation logic without redeploying the application. Another common situation where the application block works well is when your validation code must work across multiple layers of the application's architecture.

In very simple cases, when you only need to validate a few objects, you may not want to incur the overhead of adding the application block.

Alternatives to Using the Validation Application Block

Alternatives to using the Validation Application Block may include using the validation capabilities that are a part of ASP.NET and Windows Forms. Another alternative for WCF and other applications that use XML data is to use the XML Schema Definition tool (XSD), which allows you to validate messages at the XML level. If your validation logic only needs to be applied within these technologies you may not need to use the application block. However, if the validation logic needs to be reused, the application block is a better choice.