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

查看:75
本文介绍了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:

组织有1个到许多环境

环境中有1台机器

然后将有一个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?

(我自己的偏见是,这些ViewModels不应相互嵌套,并且ViewModels实际上应对应于Views,而不是域对象.我发现它与一些可维护性问题相当混乱.但是我d想知道别人的想法或经历.)

(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.)

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

推荐答案

视图模型应尽可能平整(尽管用于整理多个相关属性的嵌套不可变对象对于整理目的也是可以的).

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天全站免登陆