Validating with Sharp Architecture and MVC 2
D
OMAIN
M
ODEL VALIDATION AND
V
IEW
M
ODEL
V
ALIDATION IS NOT THE SAME
.
-
The domain model validation is inherent to our domain entities and business.
o
We use the
IsValid()
method available on every
Entity
to make sure the object data isvalid before processing.-
The view model validation is inherent to what we are presenting to the user.
o
It’s possible we are using the same object validation for the domain and the view, likethe following example.
o
But it’s more common to create an specific view for the page we are working on .
o
That means that we will probably transform that view to a know object model and weneed to validate it separately. It’s also a good practice for public forms to avoid injectmalicious form values that could end because of the automatic binder in the objectmodel.
Configure Sharp Architecture to use DataAnnotationsModelValidator insteadof NhibernateValidator
Find the current model provider and replace with the following line on Global.asax:
ModelValidatorProviders.Providers.Add(new DataAnnotationsModelValidatorProvider());
Decorate our Model entities with the corresponding DataAnnotationsattributes we need.
usingSystem.ComponentModel.DataAnnotations;public class Content:Entity
{[Required][StringLength(10)]public virtual stringTitle
{get{returntitle; }
set{ title =value; }
}[Required][StringLength(10)]public virtual stringBody
{get{returnbody; }
set{ body =value; }
Add a Comment