为什么要使用的ViewModels? [英] Why do we use ViewModels?

查看:233
本文介绍了为什么要使用的ViewModels?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始工作作为Web开发人员。我用ASP .NET MVC 4和NHibernate工作。

I have recently started working as a web developer. I work with ASP .NET MVC 4 and NHibernate.

在我工作的地方,我们都严格制作使用的ViewModels传输数据来来回回控制器和视图之间。和的ViewModels不应该包含模型的任何对象。
我明白,这是一种在控制器和视图之间的梯队。

At my work-place, we are strictly made to use viewmodels to transfer data to and fro between a controller and a view. And the viewmodels are not supposed to contain any object of a model. I understand that it is a sort of a tier between the controller and the view.

但我觉得重复和冗余写一个视图模型类,即使我们可以直接发送模型的对象视图(在大多数情况下)。

But I find it repetitive and redundant to write a viewmodel class even if we can directly send the model's object to the view (in most cases).

例如,如果我想显示的订单我可以在控制器的动作做到这一点 -

For example, if i want to display an order i can do this in the controller's action -

return View(Repository.Get<Order>(id));

但是,相反,我必须写一个视图模型,以获取订单填写,然后将其传递给视图。

But instead, I have to write a viewmodel, fill it with the fetched order and then pass it to the view.

所以,我的问题是,什么样的目的确实写的ViewModels服务时,我们可以使用模型的对象,因为它是?

So, my question is, what purpose does writing viewmodels serve when we can use the model's object as it is?

推荐答案

对于较小的项目,你说得对。我听到了你的论点和同情 - 但也有很好的理由,drudged和重复性的工作,特别是在大型,更复杂的应用程序:

For smaller projects, you're right. I hear your argument and sympathise - however there are good reasons for this, drudged and repetitive work, especially in larger and more complicated applications:


  • 这是必不可少的控制器的动作中完成所有处理。然而,在你给的例子中, Repository.Get 方法可能会返回一个懒洋洋地评估的IQueryable 对象,该对象将意味着数据库不会被打,直到视图进行评估。出于各种原因,这是不好的。 (一种解决方法是调用 .ToList 同时还在控制器)。

  • 视图不应包含任何非presentational逻辑和你不应该相信的视图(因为视图可以是用户提供)。通过提供一个Model对象(可能仍然连接到活动DatabaseContext)视图可以使恶意更改数据库。

  • 视图的数据,以显示并不总是映射1:1的模型数据,例如考虑用户详细信息页面:

  • It's essential to perform all processing within the Controller's action. However in the example you've given, the Repository.Get method might return a lazily-evaluated IQueryable object, which would mean the DB wouldn't be hit until the View is evaluated. For a variety of reasons this is bad. (A workaround is to call .ToList while still in the controller).
  • "A view should not contain any non-presentational logic" and "You should not trust the View" (because a View could be user-provided). By providing a Model object (potentially still connected to an active DatabaseContext) a view can make malicious changes to your database.
  • A View's data-to-display does not always map 1:1 with its Model's data, for example consider a User Details page:

一个用户EF Model对象重新presents的实体在数据库中,所以它可能看起来像这样:用户{用户ID,用户名,PasswordHash,PasswordSalt,EmailAddress的,CreatedDate} ,而用户详细信息页面上的字段将是用户{用户ID,用户名,密码,ConfirmYourPassword,EmailAddress的} ,你看到的区别? ERGO,你的无法的使用EF用户模型视图模型,你必须使用一个单独的类。

A User's EF Model object represents its entity in the database, so it probably looks like this: User { UserId, UserName, PasswordHash, PasswordSalt, EmailAddress, CreatedDate }, whereas the fields on a "User details" page are going to be User { UserId, UserName, Password, ConfirmYourPassword, EmailAddress }, do you see the difference? Ergo, you cannot use the EF User model as the view model, you have to use a separate class.


  • 模型操作的危险:如果你让ASP.NET MVC(或任何其他框架)做模型绑定到传入HTTP POST请求,然后(以上面的用户信息的例子),用户可以通过伪造重置任何人的密码在用户ID 属性值。 ASP.NET将绑定在重写值,除非您特别清理它(这将是多么制定个体化的ViewModels反正作为drudgeful),那么这个漏洞仍然存在。

  • 在与多个开发人员在一个团队工作的情况下,的是项目重要的是,一切都是一致的的。因为球队不共享Concious酒店心中,东西都被记录和一般化妆的意义上,它并不一致有使用定制的ViewModels但使用EF型号其他页面的某些页面。出于同样的原因,一个开发人员可以逃脱没有把过多的XML文档在他的源代码code,但在队的情况,如果你不这样做。
  • 您会土崩瓦解
  • The dangers of model manipulation: if you let ASP.NET MVC (or any other framework) do the model binding to the incoming HTTP POST Request then (taking the User details example above), a user could reset anyone's password by faking the UserId property value. ASP.NET will rewrite that value during binding and unless you specifically sanitize it (which will be just as drudgeful as making individual ViewModels anyway) then this vulnerability will remain.
  • In projects with multiple developers working in a team situation, is is important that everything is consistent. It is not consistent to have some pages using bespoke ViewModels but other pages using EF Models because the team does not share a concious mind, things have to be documented and generally make-sense. For the same reason a single developer can get away without putting excessive XML documentation in his source code, but in a team situation you'll fall apart if you don't.

有是你的情况有轻微的解决方法,我会与大家分享,但请注意preconditions:

There is a slight workaround in your case I'll share with you, but please note the preconditions:


  • 您的意见可以完全信任

  • 您的观点仅包含presentational逻辑

  • 您的应用程序主要是CRUD

  • 您的意见对应1:1,每个EF实体模型(即无连接)

  • 您的意见只能处理一个唯一的简单模型POST形式,而不是复杂的模型(即一个对象图)

...那么你可以这样做:

...then you can do this:


  • 将所有单向,非表单相关的数据到你的的ViewData 收集,或 ViewBag 中MVC 4(甚至是普通的的ViewData&LT; T&GT; 如果你是骨灰级)。这是用于存储HTML页面标题和母版页共享数据非常有用。

  • 使用您的充分评估和装的EF模型作为你的查看&LT;的TModel方式&gt; 模式

  • Put all one-way, non-form-related data into your ViewData collection, or the ViewBag in MVC 4 (or even a generic ViewData<T> if you're hardcore). This is useful for storing HTML page titles and sharing data with Master pages.
  • Use your fully-evaluated and loaded EF models as your View<TModel> models.

但是,使用这种方法谨慎,因为它可能会引入不一致性。

But use this approach with caution because it can introduce inconsistency.

这篇关于为什么要使用的ViewModels?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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