|
Validate Business Objects
Declaratively
Take advantage of .NET attributes to provide robust validation for your business objects, while generating user-interface validation automatically.
by Steve Michelotti
June 12, 2006
Technology Toolbox: C#, ASP.NET, Windows Forms, Reflection
One of the many benefits that object-oriented programming gives you is the ability to protect an object’s private members by encapsulating them inside public properties.
However, it isn’t enough to encapsulate the field; you must also perform validation in your property setters. Otherwise, you aren’t doing much to "protect" your object’s data.
I’ll show you how to construct the Declarative Validation framework to validate business objects by decorating class properties with attributes. The framework consists of six built-in validator attributes: RequiredValidator, LengthValidator, RegexValidator, RangeValidator, CompareValidator, and CustomValidator. You can plug the framework into any architecture. It comes with two abstract base classes that you can use to make implementation trivial by querying the IsValid property. You can incorporate the framework in different ways. For example, you might want to perform immediate checks after every property is set in a Windows application, but check only in the postback when writing a Web application.
ASP.NET introduced validation controls that provide rich client-side and server-side validation. However, best practices dictate that you perform data checking on your business objects. You cannot blindly trust the UI developer to perform all necessary validation—even when you are the UI developer! For example, assume a malicious user finds a way to bypass the UI constraints. Or, consider what happens if you develop a Windows Forms application where you cannot utilize the handy ASP.NET validator controls. The Declarative Validation framework can take advantage of the attributes defined on class properties to generate ASP.NET validator controls for Web applications automatically. This saves you significant time as a developer, while reducing duplicate code logic. You can also incorporate the framework’s attributes automatically in Windows applications by leveraging the System.ComponentModel.IDataErrorInfo interface. This means you will never have to write another line of validation code in the UI layer again.
The Declarative Validation framework consists of three major components. The first is the static Validation class that contains all data checking methods. The second component is the collection of validation attributes that all derive from a common base class: ValidatorAttribute. The third component is the ValidationManager, which is responsible for interpreting and executing all validator attributes.
The Validation class is a static class. This is a new feature in C# 2.0. The C# compiler enforces that all members of the class are static and marks the class as sealed and abstract at compile time. This prevents direct instantiation or inheritance. The Validation class’s sole job is to provide methods for verifying data. The validators consume these methods, but you can use the methods anywhere in your solution.
Inside the Validation Class
The Validation class includes several generic methods. These generic methods enable type-safe code reuse across disparate types. For example, Int32 implements the IComparable<int> interface. Therefore, the generic validation method for comparisons can be reused in a type-safe manner for Int32, DateTime, Decimal, Guid, and so on, due to the generic constraints:
public static bool IsCompareValid<T>
(T value, T comparisonValue,
ValidationCompareOperator
oper) where T : struct,
IComparable<T>
{
int compareResult =
left.CompareTo(right);
return IsCompareResultValid(
compareResult, oper);
}
Back to top
|