Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: using EF entities as viewmodels? [duplicate]

Possible Duplicate:
ASP.NET MVC - Linq to Entities model as the ViewModel - is this good practice?

Is is OK to use EF entities classes as view models in ASP.NET MVC?

What if viewmodel is 90% the same of EF entity class?

Let's say I have a Survey class in Entity Framework model. It 90% matches data required for view to edit it. The only difference from what view model should have - is one or several properties to be used in it (that are required to populate Survey object because EF class cannot be directly mapped onto how it's properties are represented (sub-checkboxes, radio groups, etc.))

Do you pass them using ViewData[]? Or create a copy of Survey class (SurveyViewModel) with new additional properties (it should be able to copy data from Survey and back to it)?

Edit: I'm also trying to avoid using Survey as SurveyViewModel property. It will look strange when some Survey properties are updated using UpdateModel or with default binder, while others (that cannot be directly mapped to entity) - using SurveViewModel custom properties in controller.

like image 980
Evgenyt Avatar asked Oct 19 '10 16:10

Evgenyt


1 Answers

I like using Jimmy Bogard's approach of always having a 1:1 relationship between a view and a view model. In other words, I would not use my domain models (in this case your EF entities) as view models. If you feel like you are doing a lot of work mapping between the two, you could use something like AutoMapper to do the work for you.

like image 104
MrDustpan Avatar answered Oct 06 '22 01:10

MrDustpan