Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Asp.net MVC, is it advisable for viewmodels to derive from domain models?

(I am using ASP.Net MVC, but this seems like a more generic MVC question)

Say you have a domain model representing a person, and you have a view for editing a person. Included in the Person domain object is a State of Residence property, and in the view you want a dropdown that lists states.

Is there any reason not to create a view model that derives from the domain model and simply includes properties for the UI spiciness the view requires? If so, why would not not want to do this?

TIA

like image 926
erg39 Avatar asked Aug 23 '10 21:08

erg39


1 Answers

This is not a recommended practice and since you are asking you should not do it. The short answer is create a unique view model for each and every view you are going to render. Maintain a 1-1 view to viewmodel relationship and as you code you will see why.

The long answer can be found here amoung other places http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx

Thank you,

R

like image 185
Firegarden Avatar answered Oct 06 '22 19:10

Firegarden