过去,我已经阅读了一些有关模型的MVC建议,指出不应将相同的模型对象用于域和视图.但是我找不到任何愿意讨论为什么不好的人.
I've read some MVC advice in the past regarding models stating that you should not reuse the same model objects for the domain and the view; but I haven't been able to find anyone willing to discuss why this is bad.
我认为,创建两个单独的模型-一个用于域,一个用于视图-然后在它们之间进行映射会造成大量重复,以及繁琐的映射代码(其中某些可能会因 AutoMapper )可能容易出错.
It is my opinion that creating two separate models - one for the domain, one for the view - and then mapping between them creates a lot of duplication, plus tedious mapping code (some of which might be alleviated by things like AutoMapper) that will likely be error prone.
为什么要为这两个问题建立一个单独的模型值得进行重复和映射代码的麻烦?
What makes having a separate model for the two concerns worth the trouble of duplication and mapping code?
推荐答案从本质上讲,两个模型是关注点分离.我希望我的视图能够基于单个模型工作.我想让我的领域模型代表我与领域专家建立的概念模型. ViewModel通常具有技术约束.域模型是关于POCO的,不受显示(查看)或持久保存(在DB中或其他数据库中)数据的技术约束的约束.
At its heart, two models is about Separation of Concerns. I want my View to work off of a single Model. I want my Domain Model to represent the conceptual model I build with the domain experts. ViewModel often has technical constraints. Domain Model is about POCO, and not being bound by technical constraints of either data shown (View) or persisted (in a DB or otherwise).
假设我在屏幕上显示了三个实体.这是否意味着我需要强加三者之间的关系?或者只是创建一个包含所有三个项目的ViewModel组件对象.使用单独的ViewModel,视图关注点与我的域分开了.
Suppose I have three entities shown on a screen. Does that mean I need to force a relationship between the three? Or just create a ViewModel component object that contains all three items. With a separate ViewModel, View concerns are separated from my domain.
更多推荐
MVC:数据模型和视图模型
发布评论