简体   繁体   English

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

[英]In Asp.net MVC, is it advisable for viewmodels to derive from domain models?

(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? 是否有任何理由不创建从域模型派生的视图模型,只包含视图所需的UI特性的属性? If so, why would not not want to do this? 如果是这样,为什么不想这样做呢?

TIA TIA

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

I would think that deriving a view model from a domain model would introduce coupling that MVC was intended to avoid; 我认为从域模型中导出视图模型会引入MVC旨在避免的耦合; 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. 我更喜欢将视图模型分开,因为这样做可以让我自由地大幅改变域模型并获得改进的编译时支持,将我的视图模型重新映射到新的域模型。

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. 使用ViewModels的一个重要原因是因为您的域实体往往很大,尖锐,复杂并且与持久性机制相关联。 All leading for them to have strange, interesting or destructive behaviors when they encounter things such as the DefaultModelBinder. 当他们遇到诸如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更新域实体的简单方法。

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. 我认为您的域模型应该是干净的,并且viewmodel可以执行它所拥有的功能。 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. 这是视图模型imho的重点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC中的ViewModel和域模型 - ViewModels and domain models in ASP.NET MVC ASP.NET MVC中的模型与ViewModel之间的交互 - Interaction between Models and ViewModels in ASP.NET MVC Asp.Net Core 从 ApplicationUser 派生的不同用户模型 - Asp.Net Core Different User Models to derive from ApplicationUser View asp.net MVC中的多个ViewModel - Multiple ViewModels in View asp.net MVC 如何在ASP.NET MVC中使用ViewModels? - How to use ViewModels in ASP.NET MVC? 使用多个模型比使用ASP.NET MVC中的ViewModel有更好的方法吗? - Is there a better way to consume multiple Models than through ViewModels in ASP.NET MVC? ASP.NET MVC - 我是否应该使用存储库模式将ViewModel写入数据库或首先将它们转换为模型? - ASP.NET MVC - Should I use the Repository Pattern to write ViewModels to the database or convert them to Models first? MVC将视图模型映射到域模型 - MVC map Viewmodels to domain models 如何在ASP.NET MVC中将Controller对象列表转换为Controller上的viewmodel - How can I convert a list of domain objects to viewmodels on the Controller in ASP.NET MVC 为什么 Display(Name = &quot;&quot;) 属性在我的一个 ViewModel 中被忽略,而它似乎在我的 ASP.NET MVC5 中的其他模型中工作? - Why is the Display(Name = "") attribute ignored in one of my ViewModels while it seems to work in my other models in ASP.NET MVC5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM