ASP.NET MVC: Nesting ViewModels within each other, antipattern or no?

独自空忆成欢 提交于 2019-11-30 21:23:44

The viewmodel should be as flat as possible (although nested immutable objects used to logically group multiple related properties are ok for tidying purposes).

Dont think of it as "the viewmodel should correspond to the view", think of it the other way around: "the view is an html representation of the viewmodel data".

ViewModel is a horrible term because its not a view and its not a model, its a representation of a resource.

If I do:

`GET /User/1`

I expect back some data representing User 1. If thats in HTML format because i sent

`Accept: text/html`

then so be it. Consider what your viewmodel would look like as XML or JSON.

Try and avoid nesting viewmodels as youre creating a dependency chain, just duplicate the properties, youre not violating DRY really

We've followed Josh Smith's article Simplifying the WPF TreeView by Using the ViewModel Pattern in the past. Though it is with WPF instead of MVC, I believe the concept is still applicable. I found it to be a reasonable mechanism for separating out the concerns of display in each context and allowing greater flexibility with re-use.

HTH

Andrew

I would say nested view models are fine. I know viewmodels are for one particular view but it's seems a bit wasteful to not make use of my Address ViewModel by nesting it within various other view models. Certainly makes updating it a lot easier and gives a nice structure.

I do try to keep mine as flat as possible though as normally 1 level of nesting caters for most scenarios I found come across.

Related question I answered here: Two models in one view in ASP MVC 3

ViewModels should be associated with UI concepts, not necessarily domain concepts. So in your example I can imagine one screen that shows the organization details and a listing of Environments; clicking an Environment brings you to a details screen which then shows a listing of the Machines; and clicking one of those brings you to a Machine Details View.

In that example there are 2 different views of an Environment or Machine, the summary view that takes up a row in the listing, and the details view that takes up the better part of a screen - each of which should have it's own ViewModel. I would probably create ViewModels for this like so:

OrganizationDetailsViewModel
----List of EnvironmentSummaryViewModels


EnvironmentDetailsViewModel
----List of MachineSummaryViewModels

MachineDetailsViewModel

Depending on how complex your UI is this nesting can get deeper. Lets imagine that you had a little graphical status indicator for the environment that looked at the status and displayed in a certain color. You could create a separate EnvironmentStatusViewModel that would then be nested within the EnvironmentSummaryViewModel and possibly also nested within the EnvironmentDetailsViewModel.

I still want to know the advantages... and disadvantages. Is it faster - or simpler to read? If you have a view that needs to render all the info isn't it simpler to eager fetch everything to display in a single DB Get instead of multiple lazy chatty connections?

In one view wouldn't the user want to see the "organization summary" which includes all associated "Environments" and "Machines". The counts of the potentially nested items if nothing else. (Not the Environment and Machine details - these merit different views)

There seems to be a leaning towards building views to serve a single responsibility (View Machine (only shows machine info) -> View Machine Detail (only shows Machine Detail) ) It would be simple enough to make this happen but there comes a time when the views are not feature rich as there is simply too little information.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!