ViewModel 最佳实践 [英] ViewModel Best Practices

查看:34
本文介绍了ViewModel 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题,看起来有一个控制器创建一个 ViewModel 更准确地反映视图试图显示的模型,但我对一些约定很好奇(我是 MVC 模式的新手,如果不是已经很明显了).

From this question, it looks like it makes sense to have a controller create a ViewModel that more accurately reflects the model that the view is trying to display, but I'm curious about some of the conventions (I'm new to the MVC pattern, if it wasn't already obvious).

基本上,我有以下问题:

Basically, I had the following questions:

  1. 我通常喜欢有一个类/文件.如果 ViewModel 只是为了将数据从控制器传递给视图而创建的,这对 ViewModel 有意义吗?
  2. 如果 ViewModel 确实属于它自己的文件,并且您使用目录/项目结构来保持独立,那么 ViewModel 文件属于哪里?在 Controllers 目录中?
  1. I normally like to have one class/file. Does this make sense with a ViewModel if it is only being created to hand off data from a controller to a view?
  2. If a ViewModel does belong in its own file, and you're using a directory/project structure to keep things separate, where does the ViewModel file belong? In the Controllers directory?

现在基本上就是这样.我可能还有几个问题要问,但这在过去一个小时左右一直困扰着我,我似乎可以在其他地方找到一致的指导.

That's basically it for now. I might have a few more questions coming up, but this has been bothering me for the last hour or so, and I can seem to find consistent guidance elsewhere.

查看 CodePlex 上的示例 NerdDinner 应用,看起来 ViewModel 是 控制器,但它们不在自己的文件中仍然让我感到不舒服.

Looking at the sample NerdDinner app on CodePlex, it looks like the ViewModels are part of the Controllers, but it still makes me uncomfortable that they aren't in their own files.

推荐答案

我为每个视图创建了我所谓的ViewModel".我将它们放在我的 MVC Web 项目中名为 ViewModels 的文件夹中.我以它们代表的控制器和动作(或视图)命名它们.因此,如果我需要将数据传递到 Membership 控制器上的 SignUp 视图,我会创建一个 MembershipSignUpViewModel.cs 类并将其放入 ViewModels 文件夹中.

I create what I call a "ViewModel" for each view. I put them in a folder called ViewModels in my MVC Web project. I name them after the controller and action (or view) they represent. So if I need to pass data to the SignUp view on the Membership controller I create a MembershipSignUpViewModel.cs class and put it in the ViewModels folder.

然后我添加了必要的属性和方法,以方便将数据从控制器传输到视图.我使用 Automapper 从我的 ViewModel 获取到域模型,并在必要时返回.

Then I add the necessary properties and methods to facilitate the transfer of data from the controller to the view. I use the Automapper to get from my ViewModel to the Domain Model and back again if necessary.

这也适用于包含属于其他 ViewModel 类型的属性的复合 ViewModel.例如,如果您在成员资格控制器的索引页面上有 5 个小部件,并且您为每个局部视图创建了一个 ViewModel - 您如何将数据从索引操作传递给局部视图?您向 MyPartialViewModel 类型的 MembershipIndexViewModel 添加一个属性,并且在呈现部分时您将传入 Model.MyPartialViewModel.

This also works well for composite ViewModels that contain properties that are of the type of other ViewModels. For instance if you have 5 widgets on the index page in the membership controller, and you created a ViewModel for each partial view - how do you pass the data from the Index action to the partials? You add a property to the MembershipIndexViewModel of type MyPartialViewModel and when rendering the partial you would pass in Model.MyPartialViewModel.

这样做可以让您调整部分 ViewModel 属性,而根本无需更改索引视图.它仍然只是传入 Model.MyPartialViewModel,因此当您所做的只是向部分 ViewModel 添加属性时,您必须遍历整个部分链来修复某些问题的可能性较小.

Doing it this way allows you to adjust the partial ViewModel properties without having to change the Index view at all. It still just passes in Model.MyPartialViewModel so there is less of a chance that you will have to go through the whole chain of partials to fix something when all you're doing is adding a property to the partial ViewModel.

我还将命名空间MyProject.Web.ViewModels"添加到 web.config,以便我可以在任何视图中引用它们,而无需在每个视图上添加显式导入语句.只是让它更干净一点.

I will also add the namespace "MyProject.Web.ViewModels" to the web.config so as to allow me to reference them in any view without ever adding an explicit import statement on each view. Just makes it a little cleaner.

这篇关于ViewModel 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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