ASP.NET MVC - 视图模型,域模型和数据模型 [英] ASP.NET MVC - view model, domain model and data model
问题描述
总之,我最终使用了三种类型的模型。这个可以吗?视图模型保存在表示层中,域模型保留在业务层中,数据模型保存在存储库层中。
请让我知道你的想法。
听起来不错,确实有好几个好处。
-
您可以从头重新创建数据库模型,而不会影响域模型,但它们当然如何映射。有些人会认为这两个可以合并成一个,但是这取决于您的设置。
-
分开的视图模型将允许您更自由地更改和创建新的视图模式,以适应您的意见。它也有助于预防晚加载代理等。
很多人也会有一个Dto的对象。这些用于缓存的一组对象,如果您有多个UI,也可以使用Windows服务。
Automapper 是非常受欢迎的,以减轻有这么多模型映射的痛苦。
I am using entity framework in my latest ASP.NET MVC 3 project. As it is DB first, the entity framework generates Database models. In my service (business) layer I Scaffold (MvcScaffolding) to generate service methods, views and controllers. Scaffolding also generates the domain models. When binding these models to the Views, I use view models.
In short, I ended up in using three types of models. Is this Ok? The view models are kept in the Presentation layer, domain models are kept in the business layer and data models are kept in the repository layer.
Please let me know your thoughts.
That sounds fine and indeed has several benefits.
You can recreate your database models from scratch without affecting the domain models, except how they are mapped of course. Some would argue that these two could be merged into one but it deeply depends on your setup.
Separate view models will allow you more freedom to change and create new viewmodels to suit your views. It also helps preventing late loading proxies etc.
Many people would also have a Dto set of objects. These come in useful as a set of objects for caching and also if you have more than one UI, say a windows service as well.
Automapper is very popular to ease the pain of having so many models to map.
这篇关于ASP.NET MVC - 视图模型,域模型和数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!