views:

77

answers:

4

We've been having a discussion at work about whether to use Domain Objects in our views (asp.net mvc 2) or should every view that requires data be sent a ViewModel?

I was wondering if anyone had any pros/cons on this subject that they could shed some light on?

Thank you

+1  A: 

If you use NHibernate or similar - your domain objects will most likely be proxies, serializing these dun work. You should always use a ViewModel and map your domain objects to DTOs within your viewmodel. Don't take shortcuts here. Setting the convention will alleviate the pain you'll suffer later on.

It's a standard pattern for a reason.

w://

cvista
+8  A: 

I like to segregate my Domain Objects from my Views. As far as I'm concerned, my Domain Objects are solely for the purpose of representing the Domain of the application, now how the application is displayed.

The presentation layer should not contain any domain logic. Everything they display should be pre-determined by their Controller. The ideal way to ensure this is always adhered to is to ensure the view only receives these flattened ViewModels.

I did ask a similar question myself. Here's a quote from the answer I accepted:

I think that there are merits to having a different design in the domain than in the presentation layer. So conceptually you are actually looking at two different models, one for the domain layer and one for the presentation layer. Each of the models is optimized for their purpose.

If I have the domain objects for Customer > Sales > Dispatch Address, then I don't want to have to deal with the object traversal in my view. I create a flattened view model that contains all of the properties. There's almost no extra work in mapping to and from this flattened view/presentation model if you use the excellent open source project AutoMapper.

Also, why would you want to pass an entire domain object back to a view if you can create an optimised representation of that model?

GenericTypeTea
+1, but what about simple domain objects, like a Category, that just has Id and Label ? For some applications with lots of these, it can become quite annoying to create MyEntity { Id, Label } and MyDto { Id, Label }, where the only thing differing is the name.
mathieu
@mathieu - Obviously you can use your discretion when it comes to examples like this :). Domain Objects such as your example would not contain any domain logic and therefor would certainly be acceptable.
GenericTypeTea
ok that's what I thought. if you have some time, could you have a look at this one : http://stackoverflow.com/questions/3539108/asp-net-mvc-model-inheritance ?I'm not really new with asp.net mvc technical stuff, but for the "philosophical" and design, I could use some pointers !
mathieu
+1  A: 

It depends. In some case it will be fine to use instances of model classes. In other cases a separate ViewModel is the better choice. In my experience it is perfectly acceptable to have different models in your domain and in your views. Or to use the domain model in the view. Do what works best for you. Do a spike for each option, see what works and then decide. You can even choose a different option for each view (and/or partial).

John
+1  A: 

There are definitely going to be simple little apps where it's fine to use the same models across all layers. Generally little forms over data apps. But for a proper domain, my thoughts on the subject are to keep the domain models and view models separate because you don't want them to ever impact each other when changed.

If the domain logic needs a small change to process some new business logic on the back end, you don't want to risk that altering your view. Conversely, if marketing or someone wants to make changes to a view, you don't want those changes leaking back into your domain (having to populate fields and maintain data for no other purpose than some view somewhere is going to use it).

David