为什么应该使用视图模型?
我是使用 ASP.NET MVC 开发 Web 应用程序的新手。事实上,无论技术如何,我对开发网络应用程序都很陌生。
目前,我正在开发一个项目,只是为了更好地了解 ASP.NET MVC 框架。当阅读 SO 和互联网上的其他地方时,共识似乎是视图永远不应该直接处理业务对象(即实现业务逻辑并包含关联属性的对象)。相反,应该使用视图模型。然而,这引入了几个问题:
- 我应该将验证代码放在哪里?
- 我需要添加代码以在业务对象和视图模型之间进行映射。
事实上,这看起来相当麻烦,而且我还没有看到有人正确解释为什么将业务对象传递给视图是一个坏主意。有人可以尝试解释一下吗(或指出一个好的解释)?
只是澄清;我并不是在寻找有关如何处理上述视图模型的两个问题的示例,而只是简单地解释为什么我应该使用视图模型。
I'm new to developing web apps using ASP.NET MVC. In fact, I'm rather new to developing web apps, regardless of technology.
Currently, I'm working on a project just to get to know the ASP.NET MVC framework better. When reading on SO and elsewhere on the internet, the consensus seems to be that the views should never deal directly with the business objects (i.e. objects implementing business logic and containing associated attributes). Instead, view models should be used. However, this introduces a couple of issues:
- Where do I put my validation code?
- I need to add code to map between business objects and view models.
In fact, it seems rather cumbersome and I haven't really seen anyone explaining properly why it's a bad idea passing business objects to the views. Could someone please try to explain this (or point to a good explanation)?
Just a clarification; I'm not looking for examples on how to handle the two issues with view models above but simply an explanation on why I should use view models at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
应使用视图模型的三个原因:
原因 1:从视图中删除逻辑
原因二:安全
原因三:松散耦合
以下链接可能有用:
http://www.codeproject.com/Articles/ 223547/为什么应该使用视图模型的三个原因
Three reasons to why you should use View Models:
Reason 1: Remove logic from your Views
Reason two: Security
Reason three: Loose coupling
Below link may useful:
http://www.codeproject.com/Articles/223547/Three-reasons-to-why-you-should-use-view-models
首先,允许视图直接访问 ASP.NET MVC 中的业务对象会带来一些额外的安全问题。当用户将值发送回控制器时,ASP.NET MVC 会为您执行大量模型绑定。这可能会让您面临各种攻击。通过在其间引入视图模型,您可以确保仅绑定您感兴趣的字段(因为视图模型将仅包含您关心的字段)。
至于其他问题:
我直接在 ViewModel 上使用 DataAnnotations。这使我能够使用 ASP.NET MVC 中内置的模型验证架构。如果有任何超出此范围的验证,我会在控制器中处理它。
真的。但是使用像 AutoMapper 这样的东西可以大大减少您必须手动编写的样板代码量。
First off, allowing the Views to have direct access to the Business Objects in ASP.NET MVC introduces some extra security concerns. ASP.NET MVC does a lot of Model binding for you when a user posts a value back to your controller. This can open you up to various kinds of attacks. By introducing a View Model in between, you can be sure that only the fields you are interesting are bound (because the ViewModel will only contain the fields you care about).
As for the other questions:
I use DataAnnotations on my ViewModels directly. That allows me to use the Model validation architecture built in to ASP.NET MVC. If there's any validation beyond that I handle it in my controller.
True. But using something like AutoMapper can greatly reduce the amount of boilerplate code that you have to write by hand.
MVC 很容易理解并且开销很小。但那些使用模型-视图-控制器模式一段时间的人知道它并不完美。甚至还差得远。模型-视图-视图模型模式提供了一个有趣的替代方案。
理解模型-视图-视图模型模式扩展了模型-视图-控制器模式非常重要。如果您习惯了 MVC,那么这并不是一个巨大的范式转变。尽管 MVVM 的优点很微妙,但意义深远。
MVVM 相对于 MVC 有哪些优势?
MVC is easy to understand and has very little overhead. But those that have used the Model-View-Controller pattern for some time know that it isn't perfect. Not even close. The Model-View-ViewModel pattern offers an interesting alternative.
It is important to understand that the Model-View-ViewModel pattern extends the Model-View-Controller pattern. It isn't a dramatic paradigm shift if you are used to MVC. Even though the advantages of MVVM are subtle, they are profound.
What are some of the advantages MVVM has over MVC?
在视图模型上,您应该验证特定于应用程序的所有内容,例如日期应该采用 en-US 格式文化,...
这就是为什么有诸如 AutoMapper 之类的工具。
当您在视图中直接使用域模型时,会出现不同的问题:
模型可能包含诸如
IsAdmin
之类的属性,然后我将使用以下签名的控制器操作的含义留给您想象:<前><代码>[HttpPost]
公共 ActionResult CreateUser(BusinessObjectUser 用户) { ... }
假设您已通过不包含此属性从基础表单中隐藏了该属性。
On the view models you should validate everything that's specific to the application like for example the date should be in en-US format culture, ....
That's why there are tools such as AutoMapper.
Different problems arise when you use directly your domain models in the views:
The models might contain properties such as
IsAdmin
and then I am leaving to your imagination the implication of a controller action with the following signature:assuming that you have hidden this property from the underlying form by not including it.