Databases Reference
In-Depth Information
CHAPTER 8
IN THIS CHAPTER
. Validation Framework
Implementing Entity
Validation
. Customizing Entity Framework
Code Generation
. Automated Testing
. Declarative Validation
. Imperative Validation
V alidation of entities is one of the key aspects of
business logic. The .NET Framework introduced a new
validation framework in Service Pack 1 of version 3.5,
at the same time the first version of Dynamic Data was
released. This framework is implemented in the
System.ComponentModel.DataAnnotations namespace and
contains a set of attributes and classes for validating objects
and their properties.
. Validating Multiple Rules
. Validating Entity State Changes
The Data Annotations namespace is defined in a separate
assembly called System.ComponentModel.DataAnnotations ,
installed in the global assembly cache. This assembly is
referenced by default when you create a new Dynamic Data
project in Visual Studio. If, however, your entity classes are
located in a separate project, make sure it references this
assembly as well.
Validation Framework
At the core of the validation framework is an abstract base
class called ValidationAttribute . It defines a virtual
method called IsValid , which takes an object to validate
and returns true if it is valid and false if it is not. Concrete
descendants of this class, such as the RequiredAttribute ,
override the IsValid method to perform the actual
validation.
The validation attributes are applied to the things they vali-
date, such as properties, fields, or entire classes. Consider
the following example that defines a simple Product entity
class with a required property called ProductName :
 
 
 
Search WWH ::




Custom Search