ASP.NET MVC:在彼此中嵌套 ViewModel,反模式还是不? [英] ASP.NET MVC: Nesting ViewModels within each other, antipattern or no?

查看:16
本文介绍了ASP.NET MVC:在彼此中嵌套 ViewModel,反模式还是不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中 ViewModel 相互嵌套,因此它们本质上是域层次结构的字符串类型复制.例如,如果我们的域具有以下关系:

I have a project where ViewModels are nested within each other such that they essentially are a string-typed replication of the domain hierarchy. For example, if our domain has the following relationships:

组织有一对多的环境

环境有一对多机器

那么就会有一个OrganizationViewModel,里面有一对多的EnvironmentViewModels,而EnvironmentViewModel本身也会有一对多的MachineViewModels.然后在整个应用程序中重复使用这种层次结构样式,使用这种类型的大约五个 ViewModel 之一.(例如,EnvironmentViewModel 用于多个页面,MachineViewModel 也用于其中许多页面,这取决于所查看的层次结构的级别......为了讨论的目的,我已经简化了这一点,但层次结构比上面的 3 个要大一点).

then there will be an OrganizationViewModel that has one to many EnvironmentViewModels in it, and the EnvironmentViewModel will have one to many MachineViewModels themselves. This style of hierarchy is then reused throughout the app, with one of about five ViewModels of this type. (e.g. EnvironmentViewModel is used for multiple pages, MachineViewModel for many of them as well, depending on the level of the hierachy being viewed...I've simplified this for purposes of discussion but the hierachy is a little larger than just the 3 above).

现在,尽管我很想从上层下来谴责这种做法,但我一直无法找到太多关于此的信息.任何人都可以向我指出有关既定做法的更多详细信息吗?趣闻分享?

Now, as much as I'd like to come down from above and condemn this practice, I haven't been able to find much information on this. Can anyone point me to more details about established practice? Anecdotes to share?

(我自己的偏见是这些 ViewModel 不应该以这种方式相互嵌套,并且 ViewModel 实际上应该对应于视图,而不是域对象.我发现它在一些可维护性问题上非常混乱.但我想知道其他人的想法或经历.)

(My own bias is that these ViewModels shouldn't be nested within each other this way, and the ViewModels should actually correspond to the Views, not the domain objects. I find it pretty messy with some maintainability issues. But I'd like to know what others think or have experienced.)

我附上了这个问题以供参考,但它描述了在视图模型中嵌套域对象,而不是在视图模型之间嵌套.

推荐答案

viewmodel 应该尽可能扁平(尽管用于对多个相关属性进行逻辑分组的嵌套不可变对象对于整理目的是可以的).

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

不要认为它是视图模型应该对应于视图",相反地想:视图是视图模型数据的 html 表示".

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 是一个可怕的术语,因为它不是视图,也不是模型,而是资源的表示.

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

如果我这样做:

`GET /User/1`

我希望返回一些代表用户 1 的数据.如果那是 HTML 格式,因为我发送了

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

`Accept: text/html`

那就这样吧.考虑一下您的视图模型作为 XML 或 JSON 的样子.

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

在创建依赖链时尽量避免嵌套视图模型,只需复制属性即可,您并没有真正违反 DRY

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

这篇关于ASP.NET MVC:在彼此中嵌套 ViewModel,反模式还是不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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