4
votes

I've been developing a web application with asp.net mvc, nhibernate and ddd concepts.

I've developed validations with Fluent Validation for my domain classes and it works fine. Well, now, I need a ViewModel to edit an entity in a View, soo, my question is, Do I need to create another validation class to validate my viewmodel? Or what should I do to get around this situation?

I ask it because I don't want to broke the DRY (don't repeat yourself) concetp.

Thanks!

1

1 Answers

6
votes

Domain level validation, and View-Model validation are quite different imho (although they can have lots of overlap).

For instance, it may be perfectly allowable to have a certain field as null in your database, but require it's input on certain webforms. In this case you would check for null within the Model validation.

It would also be quite normal for multiple client applications to share the same Domain controllers (via WCF for example), but to possess different application validation logic.

If you use DataAnnotations in your view model you can get client-side javascript validation for free, so as a general rule, I always have a separate ViewModel from my Domain objects, even if it's a 1:1 mapping - I just use AutoMapper to translate between them. In addition to getting the client-side validation, it also reduces the clutter within the Domain validation.