当使用视图模型将 EF 实体与视图解耦时,将 EF 实体作为域模型?
我正在尝试了解我的 MVC2 站点的最佳架构。
当我一直在尝试使用实体框架将数据传入和传出数据库时,我开始意识到我迄今为止构建的简单域模型并不能满足我计划视图的所有需求。所以我正在考虑遵循这个问题的公认答案: 为什么有两个类,视图模型和域模型?。
但是,我认为领域模型和 EF 模型之间似乎存在冗余,但几乎没有什么回报,而且我什至无法理解概念上的差异。我没有要求将来需要切换数据源,而且我也没有预见到需要切换我的 ORM 解决方案。
问题:
如果我遵循这种模式,那么由于我使用的是实体框架,我是否应该只使用我的 EF 实体直接用作域模型? (注意:我还没有考虑过“如何”,但也欢迎回答。)或者我仍然建议管理一组单独的域模型?
I'm trying to understand the best architecture for my MVC2 site.
As I have been experimenting with getting the data in and out of a database with Entity Framework, I am beginning to realize the simple domain-models I have so far constructed do not map to all the needs of my planned views. So I am considering following the accpepted answer to this question: Why Two Classes, View Model and Domain Model?.
But there seems to be redundancy with little payoff that I can perceive between the domain-models and the EF models, and I can't even hardly understand the conceptual difference. I do NOT have as a requirement the need to switch data sources down the road, and I do not forsee the need to switch my ORM solution either.
QUESTION:
If I follow this pattern then, since I am using Entity Framework, shouldn't I just use my EF entities to serve directly as the domain models? (note: I haven't thought through the "how" of that, but answers there are welcome too.) Or am I still advised to manage a separate set of domain-models?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来你这里有一些冗余。读你的段落:
我认为 EF 模型和领域模型之间没有真正的区别。在我创建的项目中,我的 EF 模型是我的域模型。
但是,我的域模型类与我的 ViewModel 不同。域模型类可能包含视图不感兴趣的数据,或者视图可能需要基于视图中的信息计算/评估的信息。一个简单的示例可能是:
在此示例中,我有兴趣在视图中显示实际的结束时间,但我没有兴趣将其存储在数据库中,因为这可能会导致数据差异(DurationInMinutes +如果数据在保存时损坏,开始可能不等于结束)
当我第一次开始以这种方式编码时,我最终做了很多手动工作,将我的域模型映射到 ViewModel,然后再映射回来。 AutoMapper 改变了这一切 :) Google 一下,或者 NuGet 一下,它会让你的生活变得更轻松 :)
希望这会有所帮助。如果我完全没有抓住重点,请发表评论:)
更新以解决评论
然后,DataAnnotations 将应用于 ViewModel,因为通常 DataAnnotations 表示数据应如何在视图中显示和验证。
例如,您可以将
[Required]
属性放在public DateTime Start {get; set;}
以便 Html.DisplayFor 扩展根据您的数据注释自动验证您的 HTML。根据定义(无论如何),域模型不应包含与您的业务逻辑相关的任何代码或逻辑。域模型只负责根据您的数据存储包含非常原始的数据。就我个人而言,我喜欢在中间放置某种服务层,负责获取数据并返回 ViewModel,也可以执行相反的操作。
最终目标是避免直接从控制器引用域模型。
当然,所有这些都必须根据项目的规模来权衡。仅仅为了模拟一个测试站点而做这一切肯定有点矫枉过正——但在任何其他项目中,你实际上要部署可能扩展、扩展或以其他方式改变的东西,这是一个很好的习惯,因为它真的很重要提高你这样做的能力。
这种方法的另一个关键点是,您被迫将操作抽象为更小、更易于管理的单元,从而实现更好、更精确的单元测试。
It seems you've got some redundancy here. Reading your paragraph:
I would argue that there is no real difference between the EF Model and your Domain Model. In the projects I create, my EF Model is my Domain model.
However, my Domain model classes are not the same as my ViewModels. The Domain model class might contain data that is not interesting for the View, or maybe the view needs information that is calculated/evaluated based on information in view. A simple example might be:
In this example I'm interested in displaying the actual End-time in my View, but I have no interest in storing it in the database, as that might lead to data-discrepencies (DurationInMinutes + Start might not equal End if data is corrupted upon saving)
When I first started coding this way, I ended up doing alot of manual work mapping my Domain models to ViewModels, and back. AutoMapper changed all that :) Google it, or NuGet it and it will make your life a whole lot easier :)
Hope this helps a little. Please comment if I'm totally missing the point :)
Update to address the comment
DataAnnotations would then be applied to the ViewModel, because normally DataAnnotations denote how the data should be displayed and validated in the View.
For instance you would put the
[Required]
attribute onpublic DateTime Start {get; set;}
in order for the Html.DisplayFor extensions automatically validates your HTML according to your dataannotations.By definition (by some anyway) the Domain Model should not contain any code or logic related to your business logic. The Domain Model is simply responsible for containing the data pretty raw according to your datastore. Personally I like to put some sort of Service layer inbetween that is responsible for fetching the data and returning ViewModels, and also doing the reverse.
The ultimate goal is to avoid referencing your domainmodel directly from your controllers.
Of course, all these points has to be weighed in reference to the size of the project. It's certainly overkill to do all this just to mock up a test-site - but in any other project where you'll actually be deploying something that might scale, expand or otherwise change, it's a good practice to get used to, as it seriously increases your ability to do so.
Another key point to this approach is that you are forced to abstract your operations down to smaller and more managable units, enabling better and more precise unit-tests.