model view controller - ASP.NET MVC: Nesting ViewModels within each other, antipattern or no? -
i have project viewmodels nested within each other such string-typed replication of domain hierarchy. example, if our domain has following relationships:
organization has 1 many environments
environment has 1 many machines
then there organizationviewmodel has 1 many environmentviewmodels in it, , environmentviewmodel have 1 many machineviewmodels themselves. style of hierarchy reused throughout app, 1 of 5 viewmodels of type. (e.g. environmentviewmodel used multiple pages, machineviewmodel many of them well, depending on level of hierachy being viewed...i've simplified purposes of discussion hierachy little larger 3 above).
now, as i'd come down above , condemn practice, haven't been able find information on this. can point me more details established practice? anecdotes share?
(my own bias these viewmodels shouldn't nested within each other way, , viewmodels should correspond views, not domain objects. find pretty messy maintainability issues. i'd know others think or have experienced.)
the viewmodel should flat possible (although nested immutable objects used logically group multiple related properties ok tidying purposes).
dont think of "the viewmodel should correspond view", think of other way around: "the view html representation of viewmodel data".
viewmodel horrible term because not view , not model, representation of resource.
if do:
`get /user/1` i expect data representing user 1. if thats in html format because sent
`accept: text/html` then it. consider viewmodel xml or json.
try , avoid nesting viewmodels youre creating dependency chain, duplicate properties, youre not violating dry really
Comments
Post a Comment