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

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

问题描述

我知道使用域模型作为视图模型可能很糟糕.如果我的域模型有一个名为 IsAdmin 的属性并且我有一个 Create 控制器操作来创建用户,那么即使我没有在我的视图中公开这样的文本字段,有人也可以更改我的表单并将其发送到一个 IsAdmin=true 表单值.如果我使用模型绑定,那么当我提交域模型时,那个人现在将成为管理员.因此,解决方案变成只公开我在视图模型中需要的属性,并使用 AutoMapper 之类的工具将返回的视图模型对象的属性值映射到域模型对象的属性值.但是我读到类上的绑定属性可用于指示模型绑定器它应该和不应该绑定哪些属性.那么制作两个独立的类(域模型和视图模型)的真正原因是什么,它们本质上代表相同的事物,然后在映射它们时产生开销?是否更多是代码组织问题?如果是,我如何受益?

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?

编辑

对于与域模型分离的视图模型,我遇到的最重要的原因之一是需要实现 MVVM 模式(基于 Martin Fowler 的 PM 模式)来管理复杂的 UI.

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 时,我会将我的域模型作为我的视图模型,但我总是遇到这样的情况,我只需要再增加一两个字段来使我的视图不那么混乱.我已经离开了 查看模型/模型生成器路线,它对我来说非常有效.我不再与我的代码作斗争,但能够在不影响域模型的情况下根据需要增强我的视图模型.

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天全站免登陆