Bestpractice - Mixing View Model with Domain Model

女生的网名这么多〃 提交于 2019-11-26 23:36:39

问题


Is it reasonable to mix view models with domain models?

So i.e. the view model object contains some domain model objects (not the other way around!)


回答1:


Generally, you will have to reference your Domain Models in your View Models, or at least load the Domain Models in the controllers and pass the information on to your View Model.

I prefer to keep Controllers and Views as simple/dumb as possible, because both Domain Models and View Models are FAR easier to test.

So, I often reference my Domain Models inside my View Models. Sometimes I use aggregation, sometimes I just copy over properties (In some projects just with plain old code, in other projects using an auto mapper)




回答2:


I tend to create separate view models that contain just what I need to be displayed in the view. AutoMapper is a create tool for making this easier.



来源:https://stackoverflow.com/questions/3094633/bestpractice-mixing-view-model-with-domain-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!