Build Post

Single Post Permalink

View Thread: MVC - Attributes on Business Objects?
  • User profile image

    [ DisplayAttribute or sometimes validation attributes ]

    IMO, Model should describe the purpose of data. These attribute describes the purpose of fields on Model so they should live in Model classes. Similarly if you have flags based fields, it's nice to have properties that get/set the corresponding bit in the field.

    In the case of MVVM, the job of describing purpose of data is often offloaded to ViewModel instead. But even in that case, validation attributes still belong to Model (much like the unique constraint in your database that's about the integrity of data)

    That said, because the child classes do not inherit attributes from the parent classes (attributes only exist as metadata), early adopters in MVC .NET projects often just add everything (I mean those attributes) to ViewModel. You need .NET v4+ to be able to use Metadata classes to describe a class (and only on components that recognize existance of metadata class attribute)