I'm creating an MVC3 application with EF4 using POCOs. I've added validation attributes to my EF entities. Now, as I'm building the views, I want to use a view model (and perhaps use AutoMapper to fill them).
The problem that I have run into is that I have to redefine my validation attributes on my view model which violates the DRY principal. For example, if I decide to change the size of a field, I have to change the MaxLength attribute on both the POCO and any view models that use it.
Is there some tricky way I can map the validation rules from my POCOs to my view model?
Personally I perform validation at view models. This is what a controller receives from a view and it is the class which contains the user input. I distinguish between two type of validation rules: surface validation and business validation. Rules such as required fields, proper formats should be enforced at the view models whereas business rules like a user with a given name already exists in the database should be validated on the model.
Also you could have different view models mapped to the same model but based on the view validation rules could vary. Because of this you won't have exactly the same validation rules on the view models.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With