Build Thread

2 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

MVC - Attributes on Business Objects?

Back to Forum: Build
  • User profile image

    I am still new to MVC. While watching the video (Introduction to ASP.NET MVC: (02) Creating and Configuring Models), they talk about using attributes to describe how a web UI might look or behave. The discussion for this question occurs between 10 and 14 minutes into the video. I am hoping to learn more about this, and possibly answer my own question in later videos, but I thought I would post it here too.

    I have learned that adding visual attributes to a business-object model is not a best practice. I often build my models according to their level of abstraction and their primary purpose, so adding attributes like DisplayAttribute or sometimes validation attributes is not something you add to a business object layer. How can I expect to have these attributes attached to such a model for an MVC application? The only way I know right now is to build an intermediate layer that inherits or adapts the business layer model and attaches the attributes to the new layer. This means I am doubling my work for without a lot of gain.


  • 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)

    Recent Achievement unlocked: Code Avenger Tier 4/6: You see dead program. A lot!
    Last modified

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.