为什么要使用视图模型? [英] Why should I use view models?

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

问题描述

我是新来的开发使用ASP.NET MVC的Web应用程序。其实,我是相当新的发展不管是技术的Web应用程序。

目前,我工作的一个项目刚结识的ASP.NET MVC框架更好。当SO和其他地方阅读在互联网上的共识似乎是,该意见应不会与业务对象直接处理(即对象实现业务逻辑和包含相关的属性)。相反,应使用图模型。然而,这引入了几个问题:


  • 我在哪里把我的验证code?

  • 我需要添加code到业务对象和视图模型之间的映射。

在事实上,它似乎相当麻烦,我还没有看到任何人正确地解释为什么它是一个坏主意传递业务对象的意见。可能有人请试图解释这个(或指向一个很好的解释)?

只是一个澄清;我不是在寻找关于如何使用视图模型之上,但只是对我为什么要在所有使用视图模型的解释。

处理这两个问题的例子
解决方案

  

我在哪里可以把我的验证code?


在视图模型,你应该验证一切的特定于应用程序例如像日期应该是EN-US格式的文化,....


  

我需要添加code到业务对象和视图模型之间的映射。


这就是为什么有工具,如 AutoMapper

当您在意见直接使用您的域模型出现不同的问题:


  • 的意见有显示数据(本地化/全球化),让你无论是与意大利面条code最终在你的观点,或者你把这个code您的模型,使他们成为其他不太可重复使用的具体要求应用程序,因为你已经与特定的presentation的东西污染了他们

  • 您有依据的观点不同的验证要求。让我们例如添加和更新的意见的情况。在添加视图将不再需要Id属性,它不会被要求,因为你会插入一个新的项目。在更新查看标识属性将被必需的,因为你会被更新现有项目。这将是很难处理这些具体的验证规则,而不视图模型。

  • 该机型可能包含的属性,如 IsAdmin 然后我要离开你的想象力具有以下签名的控制器操作的含义:

      [HttpPost]
    公众的ActionResult CREATEUSER(BusinessObjectUser用户){...}

    假设你拥有的隐藏的该属性从底层形式,不包括它。


  • 而UI可能会更频繁地更换商业模式不经常改变。如果你的客户要求你一分为二,你的屏幕?这就是你present的信息变化,它被格式化也会改变的方式。如果你直接使用你的模型到你的意见spaghetiness成为与每一个变化越来越糟糕的意见。

  • 将不会被要求约60%,我在asp.net MVC的标签上的StackOverflow回答这个问题的,如果OP已经使用视图模型。

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.

解决方案

Where do I put my validation code?

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, ....

I need to add code to map between business objects and view models.

That's why there are tools such as AutoMapper.

Different problems arise when you use directly your domain models in the views:

  • The views have specific requirements for displaying the data (localization/globalization) so you either end up with spaghetti code in your views or you put this code on your models so that they become less reusable in other applications because you have polluted them with specific presentation stuff
  • You have different validation requirements based on the view. Let's take for example the case of Add and Update views. In the Add view the Id property won't be needed and it won't be required because you will be inserting a new item. In the Update view the Id property would be required because you would be updating an existing item. It would be difficult to handle those specific validation rules without view models.
  • 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:

    [HttpPost]
    public ActionResult CreateUser(BusinessObjectUser user) { ... } 
    

    assuming that you have hidden this property from the underlying form by not including it.

  • The business models don't change often whereas the UI could change more often. What if your customer asks you to split your screen in two? The way you present the information changes and the way it is formatted also change. If you use your models directly into the views the spaghetiness of your views becomes worse and worse with every change.
  • About 60% of the question I am answering on StackOverflow in the asp.net-mvc tag wouldn't have been asked if the OP have used a view model.

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

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