为什么两个类,视图模型和领域模型? [英] Why Two Classes, View Model and Domain Model?

查看:290
本文介绍了为什么两个类,视图模型和领域模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是不好用的域模型作为视图模型。如果我的域模型有一个属性名为IsAdmin,我有一个创建控制器动作来创建用户,有人可以改变我的表格,并把它张贴IsAdmin =真实的形式价值,即使我没有在我看来揭露这种文本字段。如果我使用的模型,则绑定时,我犯了我的域模型,这个人现在是一个管理员。因此,溶液变成只露出我需要在视图模型的属性和使用像AutoMapper工具来我返回视图模型对象的属性值映射到我的域模型对象。但我读了一个类的绑定属性可以用来指示模型绑定的属性它应该和不应该进行绑定。那么到底是怎样制作两个独立的类(域模型和视图模型)是必不可少的重新present同样的事情,然后在其映射incure开销的原因是什么?是更多的是code组织的问题,如果是这样,我怎么受益?

I know it could be bad to use domain models as view models. If my domain model has a property named IsAdmin and I have a Create controller action to create users, someone could alter my form and get it to POST a IsAdmin=true form value, even if I did not expose such a text field in my view. If I'm using model binding then when I committed my domain model, that person would now be an admin. So the solution becomes exposing just the properties I need in the view model and using a tool like AutoMapper to map the property values of my returning view model object to that of my domain model object. But I read that the bind attribute on a class can be used to instruct the Model Binder which properties it should and shouldn't bind. So what really is the reason for making two separate classes (domain model and view model) that essential represent the same thing and then incure overhead in mapping them? Is it more a code organization issue and if so, how am I benefiting?

修改

一个我遇到了一个视图模型这是从域模型独立的最重要原因是实现管理复杂的UI MVVM模式(基于Martin Fowler的PM模式)的需要。

One of the most important reasons I've come across for a View Model that's separate from the Domain Model is the need to implement the MVVM pattern (based on Martin Fowler's PM pattern) for managing complex UIs.

推荐答案

我发现,虽然我的域模型得到我的方式85%至有我想要的领域,它从来没有覆盖我想要的值的100%对我的看法。尤其是当它涉及到权限,以及是否用户应该能够访问该视图的某些部分。

I have found that while my domain model gets me 85% of the way to having the fields I want, it has never covered 100% of the values I want on my view. Especially when it comes to permissions and whether or not a user should have access to certain portions of the view.

设计概念,我试图遵循的是有我的看法尽可能少的逻辑。这意味着我有我喜欢CanViewThisField或视图模型字段CanEditThisField。当我第一次开始与MVC我有我的域模型是我的视图模型,我总是跑到我的地方只需要一个或两个字段,使我的观点更简洁的方案。我从那以后的<一个href=\"http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx\">View型号/模型构建器路由,并已奇妙的为我工作。我不战我的code下去,但我能够提高我的视图模型,因为我需要在不影响域模型。

The design concept I attempt to follow is to have as little logic in my views as possible. This means I have fields in my view model like "CanViewThisField" or "CanEditThisField." When I first started with MVC I would have my domain model be my view model and I was always running into the scenario where I needed just one or two more fields to make my view less cluttered. I've since gone the View Model/Model Builder route and it has worked wonderfully for me. I don't battle my code any longer but am able to enhance my view model as I need to without affecting the domain model.

这篇关于为什么两个类,视图模型和领域模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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