视图模型的最佳实践

问题描述:

问题,它看起来是有道理的有控制器创建一个视图模型更准确地反映该视图正试图以显示模式,但我很好奇一些公约(我是新来的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. 我通常喜欢有一个类/文件。是否有视图模型如果只被创建它从控制器的数据移交给一个看法?
  2. 这有意义
  3. 如果一个视图模型不属于它自己的文件,并且你正在使用的目录/项目结构让事情分开,哪里在视图模型文件属于?在控制器目录?

  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.

编辑:
在研究样本的NerdDinner应用上codePLEX,它看起来像的ViewModels是的控制器的,但它仍然让我不舒服,他们是不是在他们自己的文件

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.

我建立我所说的视图模型为每个视图。我把它们放在一个文件夹中叫的ViewModels在我的MVC的Web项目。我控制器和动作(或视图),他们重新present后,他们的名字。所以,如果我需要将数据传递给会员控制器我创建了一个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.

这也很好地工作在含有其他的ViewModels类型的属性组合的ViewModels。例如,如果你有在籍控制器的索引页5小部件,以及创建一个视图模型为每个局部视图 - 你怎么从索引动作谐音传递数据?您添加属性类型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所以少一个偶然的机会,你将不得不通过谐音的整个链条修复某些东西时,你正在做的是增加一个属性,以局部视图模型。

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,以便让我引用它们中的任何视图,而每个视图曾经加入一个明确的import语句。只是使它干净了一点。

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.