在 Asp.net MVC 中,是否建议视图模型从域模型派生?

发布于 2024-09-16 04:02:37 字数 225 浏览 4 评论 0原文

(我正在使用 ASP.Net MVC,但这似乎是一个更通用的 MVC 问题)

假设您有一个代表一个人的域模型,并且您有一个用于编辑一个人的视图。 Person 域对象中包含 State of Residence 属性,并且在视图中您需要一个列出州的下拉列表。

是否有任何理由不创建一个派生自域模型并仅包含视图所需的 UI 功能的属性的视图模型?如果是这样,为什么不愿意这样做呢?

TIA

(I am using ASP.Net MVC, but this seems like a more generic MVC question)

Say you have a domain model representing a person, and you have a view for editing a person. Included in the Person domain object is a State of Residence property, and in the view you want a dropdown that lists states.

Is there any reason not to create a view model that derives from the domain model and simply includes properties for the UI spiciness the view requires? If so, why would not not want to do this?

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

韬韬不绝 2024-09-23 04:02:37

我认为从域模型派生视图模型会引入 MVC 旨在避免的耦合;然而,话虽如此,请做对您的应用程序最有意义的事情。

我更喜欢将视图模型分开,因为这样做可以让我自由地显着更改域模型,并获得改进的编译时间支持,以将视图模型重新映射到新的域模型。

I would think that deriving a view model from a domain model would introduce coupling that MVC was intended to avoid; however, that said, do what makes the most sense for your application.

I prefer to have view models separate because doing so leaves me free to dramatically change the domain model and get improved compile time support for remapping my view model to the new domain model.

落花浅忆 2024-09-23 04:02:37

这不是推荐的做法,既然您这么要求,您就不应该这样做。简短的答案是为您要渲染的每个视图创建一个独特的视图模型。保持 1-1 视图与视图模型关系,当您编码时,您就会明白原因。

可以在其他地方找到长答案 http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx

谢谢,

R

This is not a recommended practice and since you are asking you should not do it. The short answer is create a unique view model for each and every view you are going to render. Maintain a 1-1 view to viewmodel relationship and as you code you will see why.

The long answer can be found here amoung other places http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx

Thank you,

R

你怎么这么可爱啊 2024-09-23 04:02:37

不,你真的不想这样做。

使用 ViewModel 的很大一部分原因是因为您的领域实体往往很大、尖峰、复杂并且与持久性机制相关。所有这些都会导致他们在遇到像 Def​​aultModelBinder 这样的东西时出现奇怪的、有趣的或破坏性的行为。

通过使用更简单的 ViewModel 类,您可以避免大量这些问题,同时还可以进一步将 UI 层与域模型解耦。

现在,您应该做的是提供简单的方法来从域实体生成 ViewModel 或从 ViewModel 更新域实体。

No, you don't really want to do this.

A big part of the reason to use ViewModels is because your domain entities tend to be big, spiky, complex and tied to persistence mechanisims. All leading for them to have strange, interesting or destructive behaviors when they encounter things such as the DefaultModelBinder.

By using much simpler ViewModel classes, you can avoid the bulk of these problems while also further decoupling your UI layer from your domain model.

Now, what you should do is provide easy means to generate a ViewModel from a Domain Entity or to updated a Domain Entity from a ViewModel.

梅倚清风 2024-09-23 04:02:37

我不同意这里的大部分建议。

我认为你的领域模型应该是干净的,并且视图模型应该做它必须做的事情。
如果您的视图需要一个人和伦敦的时间,我不认为这样做有问题:

ExampleViewModel : Person {
 Public DateTime LondonTime { get; set;}
}

或者

AnotherViewModel 
{
 Public Person SomeGuy { get; set;}
 Public List<Kitty> Cats{ get; set;}
}

如果您的视图需要一个人和一个小猫列表

这可以保持您的域干净;伦敦时间与人无关,但在您看来,您仍然需要获取数据。恕我直言,这就是视图模型的全部观点。

I disagree with most of the advice here.

I think that your domain model should be clean and a viewmodel does what it has to.
If your view needs a person and the time in London i dont see the problem with doing this:

ExampleViewModel : Person {
 Public DateTime LondonTime { get; set;}
}

Or

AnotherViewModel 
{
 Public Person SomeGuy { get; set;}
 Public List<Kitty> Cats{ get; set;}
}

If your view needs a person and a list of kitties

This keeps your domain clean; the time of London is not related to a person however on your view you still need get the data. This is the whole point of a view model imho.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文