Data Annotations on ViewModels or Domain Objects

Posted by Ahmad on Stack Overflow See other posts from Stack Overflow or by Ahmad
Published on 2010-06-11T09:36:40Z Indexed on 2010/06/11 9:42 UTC
Read the original article Hit count: 280

Where would data annotations be more suitable:

  1. ViewModels or
  2. Domain Objects or
  3. Both

I am struggling to decide where these will be more suited. I have not as yet fully utilized them but this question came to mind. From most of the examples I have seen, they are generally placed on Models and simply use the required attributes for validation using ModelState.IsValid. I have also seen another question on SO where the use of data annotations alone is not sufficient and advocate.

Option 1 - I will still need to validate again in my service layer. ( I think that my service layer should be complete and this include validation, since its planned to be used elsewhere)

Option 2 - How will I then get the benefits of the built in validation both client and server side.

Option 3 - there will be a repetition of validation logic, however I was wondering if one could use a MetaData class approach that can be used for both ViewModels and Domain Objects. ( This is completely of the top of my head, so it may be nonsensical)

I wonder if this question even makes sense. If not, can someone please help in understanding this better. Have I completely misunderstood the use of data annotations?

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about viewmodel