IValidatableObject interface in .Net Framework4 (System.ComponentModel.DataAnnotations)

Along with DataAnnotations validations .NET framework brought us the IValidatableObject interfaces which let us built the type level validations rules. Beginning with MVC3 IValidatableObject is honored by MVC3 validation API and EntityFramework validation API will also check any IValidatableObject.
Here it is bits of code showing the Person class implements the interface along with validate method. One simple rule checking here is the Name matches with Email address if they do the validation fail and method returns the validation result.
IValidate

Let see how this works with the automatic save changes validation before we really dig in to see what’s going on.

valExee

Above is a bit of codes for inserting Person in to the db using Enityframework which results a two validation errors .The first one is Name cannot match Email associated with the property Name and the second is same error Name cannot match Email ,this time with property name Email. Why that is?
When we look in to validate method in the domain class I built this validation result I gave it not only the error message that I want to be displayed list of the property is associated with it, this is literally the property names. The reason those property names are there is because If I am going to be take the advantage of one of the framework that is client side validation in MVC. MVC is going to read this validation result and to apply error message next to where the Name property is to be displayed and where the Email property is to be displayed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s