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. We’ll take a look at using the OnChanging partial methods from the data model to provide custom validation for Dynamic Data applications.
Validation Basics w/Dynamic Data:
Using a LINQ to SQL model based on the Northwind database, we can see there are files named Northwind.dbml and Northwind.designer.cs (and a .layout file, not applicable to this). These are the data model visual designer file and the data model’s entity classes, respectively, as shown below:
If we take a look in the Northwind.designer.cs file and find the Product class we’ll see the following methods available in a code region:
#region Extensibility Method Definitions partial void OnLoaded(); partial void OnValidate(System.Data.Linq.ChangeAction action); partial void OnCreated(); partial void OnProductIDChanging(int value); partial void OnProductIDChanged();
. . .
partial void OnReorderLevelChanging(System.Nullable<short> value); partial void OnReorderLevelChanged();
. . .
#endregion
These extensibility methods are available for each class as well as the DataContext object in your data model, and you will use these methods to implement business logic or validation in a separate partial class with these partial method definitions.
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 the the partial methods defined in the data model. To do so, we first want to add a file to extend the partial classes defined in the data model, so that any changes made to the data model’s design surface (Northwind.dbml) won’t affect our code. The file can be named anything and for this example it is named NorthwindExtended.cs.
In the NorthwindExtended.cs file, you will need to create a definition for a Product partial class and OnReorderLevelChanged method. The OnReorderLevelChanged, or any of the OnColumnChanged methods can be implemented in the the partial class definition to add any business logic or custom validation needed. If you are going to use validation code here it will generally be more complex than when you would annotate the data model with attributes for validation. In some cases a mixture of attributes and partial method validation will be the best solution. Below is a sample of code that applied the above business rules in the partial class/method found in the NorthwindExtended.cs file:
partial void OnReorderLevelChanged() { if (!this._Discontinued) { if (this._ReorderLevel <= 5 || this._ReorderLevel >= 50) { throw new Exception("The product's reorder level must be greater than 5 and less than 50"); } } else
{ throw new Exception("The product's reorder level cannot be changed if the product is discontinued"); } }
The exception that is raised by this code will be caught and displayed to the user by the DynamicValidator control located in the corresponding dynamic field template. The field template in this case is the Integer_Edit.ascx file, which maps to the smallint numeric data type defined for the ReorderLevel column. The results of the code are shown below:
Summary
You can use the partial methods of partial classes that extend the data model, giving it the capability to provide more complex validation and business logic than by decorating the properties with attributes. The above code could be refactored and trimmed so that the RangeAttribute is applied to the ReorderLevel column and the only necessary code would check to see if the product has been discontinued. This post describes how to annotate the data model with attributes for quick and easy validation.
