Custom Validation in ASP.NET Dynamic Data using Attributes

You have two choices for validation in ASP.NET Dynamic Data: Annotating the data model with attributes or extending the data model using partial methods (see here). We’ll take a look at annotating the data model to provide validation for Dynamic Data applications.

Attribute Based Validation:

Dynamic Data uses attributes to provide a quick and easy way to apply simple validation rules to properties, effectively reducing code and giving the developer a declarative based validation model for validation tasks. The attributes are found in the System.ComponentModel.DataAnnotations namespace which is shown below:

dataannotations

You can apply these attributes by creating a partial class to extend the entity classes found in your data model, plus a second helper partial class where the attributes are applied . You need the helper class because currently partial properties are not supported in C# or VB.NET, and to associate the classes you can annotate the primary partial class with the MetaDataTypeAttribute

The MetaDataTypeAttribute’s constructor accepts the type of the helper class as its only argument, and this is how the two metadata classes are associated. The helper class that the MetaDataTypeAttribute points to can be a separate class altogether or it can be nested inside the Product class for easier maintenance. 

[MetadataType(typeof(Product_Meta))]
 public partial class Product
{ public partial class Product_Meta {...} }

 

You can then add partial methods to the class and properties to the helper class; but note that when properties are defined in the helper class they should be of type Object, so that the data model can determine the actual data type itself based on the schema from the underlying database. Once you have the property defined in the helper class, you can then annotate it with one of the available attributes.

A Validation Example:

Let’s say we have two business requirements that state:

1) the reorder level of a product must be greater than 5 units and less than 50.

2) the product cannot be discontinued to set the reorder level.

We can apply these rules by using both partial methods with custom business logic to deal with rule #2 and a RangeAttribute to deal with rule #1. The following example demonstrates using the RangeAttribute to ensure that the ReorderLevel column falls between 5 and 50.

 public partial class Product_Meta 
 {
     [Range(5, 50, ErrorMessage = "The product's reorder level must be greater than 5 and less than 50")]
     public object ReorderLevel { get; set; }         
 }  

 

 

 

If you’ve read this post on custom validation you can see that the code in the partial method used to validate rule #2 can be refactored down to a single RangeAttribute in this case. Before writing any business logic code, you might want to take a look and see what attributes are available, both from Dynamic Data and your data model provider (LINQ to SQL, Entity Framework, etc…). 

Just as is the case with validation via partial methods, the DynamicValidator control will catch the exception raised when the user tries to enter a number outside the range, and the DynamicValidator control will display the error message to the user similar to the results below:

image

Summary:

Using attributes to annotate your data model for validation gives you extra flexibility and less code to deal with than you’d normally have in an application. Dynamic Data provides several useful attributes out of the box for providing validation including required, range, regular expression, data type, display and others.

#1 dfg4356345 on 11.06.2008 at 6:52 AM

egerterteeeeeeeeeeeeee