gpt4 book ai didi

asp.net - View 中的域对象

转载 作者:行者123 更新时间:2023-12-02 11:33:13 24 4
gpt4 key购买 nike

我们在工作中一直在讨论是否在我们的 View (asp.net mvc 2) 中使用域对象,还是应该将每个需要数据的 View 发送给 ViewModel?

我想知道是否有人对这个主题有任何优点/缺点可以阐明?

谢谢

最佳答案

我喜欢将域对象与 View 分开。就我而言,我的域对象仅用于表示应用程序的域,以及应用程序的显示方式。

表示层不应包含任何领域逻辑。他们显示的所有内容都应该由他们的 Controller 预先确定。确保始终遵守这一点的理想方法是确保 View 仅接收这些扁平化的 ViewModel。

我确实问过similar question我。这是我接受的答案的引用:

I think that there are merits to having a different design in the domain than in the presentation layer. So conceptually you are actually looking at two different models, one for the domain layer and one for the presentation layer. Each of the models is optimized for their purpose.

如果我有 Customer > Sales > Dispatch Address 的域对象,那么我不想处理在我看来,对象遍历。我创建了一个包含所有属性的展平 View 模型。如果您使用优秀的开源项目AutoMapper,则在映射到这个扁平 View /表示模型或从这个扁平化 View /表示模型映射时几乎不需要额外的工作。 .

此外,如果您可以创建该模型的优化表示,为什么要将整个域对象传递回 View ?

关于asp.net - View 中的域对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3530840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com