ASP.NET MVC - 视图模型,域模型和数据模型 [英] ASP.NET MVC - view model, domain model and data model

查看:139
本文介绍了ASP.NET MVC - 视图模型,域模型和数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我最新的ASP.NET MVC 3项目中使用实体框架。由于它是DB首先,实体框架生成数据库模型。在我的服务(业务)层I脚手架(MvcScaffolding)中生成服务方法,视图和控制器。脚手架还会生成域模型。将这些模型绑定到视图时,我使用视图模型



总之,我最终使用了三种类型的模型。这个可以吗?视图模型保存在表示层中,域模型保留在业务层中,数据模型保存在存储库层中。



请让我知道你的想法。

解决方案

听起来不错,确实有好几个好处。


  1. 您可以从头重新创建数据库模型,而不会影响域模型,但它们当然如何映射。有些人会认为这两个可以合并成一个,但是这取决于您的设置。


  2. 分开的视图模型将允许您更自由地更改和创建新的视图模式,以适应您的意见。它也有助于预防晚加载代理等


很多人也会有一个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.

  1. 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.

  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆