Partager via


When Should I Use the Validation Application Block?

You should consider using the Validation Application Block if you want to encapsulate validation good practice 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.

In addition, consider using the block when your validation code must work across multiple layers of the application's architecture. By defining rules in the configuration for each segment, you can reuse the same rule sets in multiple locations within your code.

Scenarios for the Validation Application Block

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:

Benefits 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 Presentation Foundation (WPF), Windows Forms, and Windows Communications Foundation (WCF) applications.

Alternatives to the Validation Application Block

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 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 (XSD) tool, 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.