ASP.NET MVC:对的ViewModels领域实体 [英] ASP.NET MVC: ViewModels versus Domain Entities

查看:98
本文介绍了ASP.NET MVC:对的ViewModels领域实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解它如何建设有MVC 3概念的应用。我previously做过的WebForms一些非常重型应用,使用一个n层的方法,通常包括域对象与库存储和存储服务之前,操作它们。

I'm building a concept application with MVC 3 in an attempt to learn its ways. I've previously done some very heavy-duty applications in WebForms, using an n-tier approach, usually consisting of domain objects with repositories for storage and services to manipulate them before storage.

我试图调和我怎么用来做事情的正确的方式做出来MVC中,如果有任何一个这样的方式。我越来越挂了比现在的事情是什么时候使用的ViewModels与何时使用都完全是另外一个项目我的域对象。验证通过的ViewModels做,但我写更多的定制,业务逻辑验证,就好像它是一个卑微的视图模型那只是那里帮助我度过库层在数据库正式收藏起来之前,中移动的数据太多责任

I'm trying to reconcile how I used to do things with the "right" way to do them in MVC, if there is any one such way. The thing I'm getting hung up over right now is when to use ViewModels versus when to use my domain objects that are in a whole other project. Validation is done with ViewModels, but as I write more customized, business-logic validation, it seems like it's too much responsibility on a lowly ViewModel that was just there to help me move data around before storing it officially in the database through the repository layer.

我也累了映射视图模型数据的官方的域对象的资源库存储和检索,但我觉得我不应该玷污我的域对象的MVC属性进行验证,无论是。

I'm also getting tired of mapping ViewModel data to the "official" domain object that the repository stores and retrieves, but I feel like I shouldn't tarnish my domain objects with the MVC attributes for validation, either.

你有在哪里画领域对象和光的ViewModels之间的界限什么建议吗?还是我复杂的东西,我的ViewModels实际上应该是官方的模型库店?

Do you have any advice for where to draw the line between domain objects and mere ViewModels? Or am I complicating things, and my ViewModels should actually be the "official" models that the repository stores?

推荐答案

我一般都默认使用视图模型,虽然只读的意见,我已经知道使用的域模型(没有理由要经过的开销映射,如果我只是要读出的数据的话)。

I generally default to using View Models, though for read-only views, I have been known to use the domain model (no reason to go through the overhead of mapping if I am only going to read data out of it).

如果你决定使用域模型,我不会让MVC直接绑定到他们,因为如果有人知道您的域名不够好,他们可以张贴绑定的属性值,你不希望用户能够编辑。您可以定义可以什么型号的粘合剂性能的黑白名单,不能结合,但采用的是别的东西,你就必须保持和东西,可以很容易地被人遗忘。

If you do decide to use domain models, I would never let MVC bind directly to them, because if someone knows your domain well enough, they can post values that bind to properties you do not want the user to be able to edit. You can define white and black list of properties of what model binder can and cannot bind to, but utilizing that is something else you'll have to maintain and something that can easily be forgotten about.

这篇关于ASP.NET MVC:对的ViewModels领域实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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