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

时间:2022-05-01 11:25:10

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

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

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.

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

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?

有没有理由不创建从域模型派生的视图模型,只包含视图所需的UI特性的属性?如果是这样,为什么不想这样做呢?

TIA

TIA

4 个解决方案

#1


7  

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.

这不是推荐的做法,因为您要求不要这样做。简短的回答是为您要渲染的每个视图创建一个独特的视图模型。维护视图模型关系的1-1视图,当您编码时,您将看到原因。

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

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

Thank you,

谢谢,

R

[R

#2


6  

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.

我认为从域模型中导出视图模型会引入MVC旨在避免的耦合;然而,那说,做你的应用最有意义的事情。

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.

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

#3


2  

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.

使用ViewModels的一个重要原因是因为您的域实体往往很大,尖锐,复杂并且与持久性机制相关联。当他们遇到诸如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.

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

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.

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

#4


-1  

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:

我认为您的域模型应该是干净的,并且viewmodel可以执行它所拥有的功能。如果你的观点需要一个人和伦敦的时间我没有看到这样做的问题:

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.

这可以保持您的域名清洁;伦敦的时间与一个人无关,但是根据您的观点,您仍然需要获取数据。这是视图模型imho的重点。

#1


7  

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.

这不是推荐的做法,因为您要求不要这样做。简短的回答是为您要渲染的每个视图创建一个独特的视图模型。维护视图模型关系的1-1视图,当您编码时,您将看到原因。

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

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

Thank you,

谢谢,

R

[R

#2


6  

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.

我认为从域模型中导出视图模型会引入MVC旨在避免的耦合;然而,那说,做你的应用最有意义的事情。

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.

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

#3


2  

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.

使用ViewModels的一个重要原因是因为您的域实体往往很大,尖锐,复杂并且与持久性机制相关联。当他们遇到诸如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.

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

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.

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

#4


-1  

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:

我认为您的域模型应该是干净的,并且viewmodel可以执行它所拥有的功能。如果你的观点需要一个人和伦敦的时间我没有看到这样做的问题:

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.

这可以保持您的域名清洁;伦敦的时间与一个人无关,但是根据您的观点,您仍然需要获取数据。这是视图模型imho的重点。