asp.net-mvc DTO = 视图模型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1982042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DTO = ViewModel?
提问by autonomatt
I'm using NHibernate to persist my domain objects. To keep things simple I'm using an ASP.NET MVC project as both my presentation layer, and my service layer.
我正在使用 NHibernate 来持久化我的域对象。为简单起见,我使用 ASP.NET MVC 项目作为我的表示层和服务层。
I want to return my domain objects in XML from my controller classes. After reading some posts here on Stack Overflow I gather DTOs are the way to go. However, I've also come across posts talking about the ViewModel.
我想从我的控制器类返回 XML 格式的域对象。在阅读了 Stack Overflow 上的一些帖子后,我认为 DTO 是必经之路。但是,我也遇到过关于 ViewModel 的帖子。
My question: Are Data Transfer Objects and ViewModels the same thing? Or is a ViewModel a kind of sub pattern of a DTO?
我的问题:数据传输对象和 ViewModel 是一回事吗?还是 ViewModel 是 DTO 的一种子模式?
回答by Daniel Auger
The canonical definition of a DTO is the data shape of an object without any behavior.
DTO 的规范定义是没有任何行为的对象的数据形状。
ViewModels are the model of the view. ViewModels typically are full or partial data from one or more objects (or DTOs) plus any additional members specific to the view's behavior (methods that can be executed by the view, properties to indicate how toggle view elements etc...). You can look at the viewmodel as all the data for a view plus behaviors. ViewModels may or may not map one to one to business objects or DTOs.
ViewModels 是视图的模型。ViewModels 通常是来自一个或多个对象(或 DTO)的完整或部分数据以及特定于视图行为的任何其他成员(视图可以执行的方法、指示如何切换视图元素的属性等)。您可以将视图模型视为视图加行为的所有数据。ViewModel 可能会也可能不会一对一地映射到业务对象或 DTO。
By the way, NHibernate projectionscome in handy if a certain viewmodel needs a subset of the data from a persisted object.
顺便说一句,如果某个视图模型需要来自持久对象的数据子集,则 NHibernate投影会派上用场。
回答by Ray
ViewModel in ASP.NET MVC practice is the same as the DTO, however ViewModel in MVVM pattern is different from DTO because ViewModel in MVVM has behaviors but DTO does not have.
ASP.NET MVC 实践中的 ViewModel 与 DTO 相同,但是 MVVM 模式中的 ViewModel 与 DTO 不同,因为 MVVM 中的 ViewModel 有行为而 DTO 没有。
回答by stiank81
DTO != ViewModel
DTO != 视图模型
In the MVVMpattern the ViewModel is used to isolate the Model from the View. To represent the Model you could use simple DTOclasses, which again is mapped to a database through e.g. NHibernate. But I've never seen a ViewModel class which is modelled as a DTO.. ViewModel classes mostly have behavior, which DTOs don't have.
在MVVM模式中,ViewModel 用于将模型与视图隔离。为了表示模型,您可以使用简单的DTO类,这些类再次通过例如 NHibernate 映射到数据库。但我从未见过建模为 DTO 的 ViewModel 类.. ViewModel 类大多具有行为,而 DTO 则没有。
回答by David
DTO - Data Transfer Objects are exactly as it says, containers for transferring data. They have no behaviour but merely a bunch of setters and getters. Some people make them immutable and just create new ones when needed rather than updating existing ones. They should be serializable to allow transfer across the wire.
DTO - 数据传输对象正如它所说,是用于传输数据的容器。它们没有行为,而只是一堆 setter 和 getter。有些人使它们不可变,只在需要时创建新的而不是更新现有的。它们应该是可序列化的,以允许跨线路传输。
Generally DTOs are used to ship data from one layer to another layer across process boundries as calls to a remote service can be expensive so all the required data is pushed into a DTO and transferred to the client in one chunk (coarse grained).
通常,DTO 用于跨进程边界将数据从一层传送到另一层,因为对远程服务的调用可能很昂贵,因此所有需要的数据都被推送到 DTO 中并以一个块(粗粒度)传输到客户端。
However, some people use the notion of screen bound DTOs (nothing to do with crossing process boundries). Again these are populated with the required data (generally the data required for a particular screen and could be an aggregation of data from various sources) and sent to the client.
然而,有些人使用屏幕绑定 DTO 的概念(与跨越进程边界无关)。同样,这些填充有所需的数据(通常是特定屏幕所需的数据,可能是来自各种来源的数据的聚合)并发送到客户端。
http://blog.jpboodhoo.com/CommentView,guid,21fe23e7-e42c-48d8-8871-86e65bcc9a50.aspx
http://blog.jpboodhoo.com/CommentView,guid,21fe23e7-e42c-48d8-8871-86e65bcc9a50.aspx
In simple cases as has already been stated this DTO can be used for binding to the view but in more complex cases it would require the creation of a ViewModel and unloading of data from DTO to ViewModel which is obviously more work (when applying MVVM pattern).
在已经说过的简单情况下,这个 DTO 可用于绑定到视图,但在更复杂的情况下,它需要创建一个 ViewModel 并将数据从 DTO 卸载到 ViewModel,这显然是更多的工作(当应用 MVVM 模式时) .
So again as already stated DTO!=ViewModel
所以再次正如已经说过的 DTO!=ViewModel
and
和
DTO and ViewModel have different purposes in life
DTO和ViewModel在生活中有不同的用途
回答by riadh gomri
First, the major difference is that ViewModel can have behaviour or methods that DTO Must Not !!!
首先,主要区别在于 ViewModel 可以具有 DTO 不能拥有的行为或方法!!!
Second, Using DTO as a ViewModel in ASP.NET MVC make your application tightly coupled to DTO and that's exactly the opposite purpose of using DTO. If you do so, what's the difference using your domain Model or DTO, more complexity to get an anti-pattern ?
其次,在 ASP.NET MVC 中使用 DTO 作为 ViewModel 使您的应用程序与 DTO 紧密耦合,这与使用 DTO 的目的完全相反。如果你这样做,使用你的域模型或 DTO 有什么区别,获得反模式更复杂?
Also ViewModel in ASP.NET can use DataAnnotations for validation.
ASP.NET 中的 ViewModel 也可以使用 DataAnnotations 进行验证。
The same DTO can have different ViewModels Mapping, and One ViewModel can be composed from differents DTO (always with object mapping not composition) . because i think it is even worse if you have a ViewModel that contains a DTO, we will have the same problem.
同一个 DTO 可以有不同的 ViewModel Mapping,一个 ViewModel 可以由不同的 DTO 组合而成(总是有对象映射而不是组合)。因为我认为如果您有一个包含 DTO 的 ViewModel,情况会更糟,我们会遇到同样的问题。
From your presentation layer, think about DTO as a contract, you will receive an object that you have to consider as stranger to your application and don't have any control on it (even if you have ex the service, the dto and presentation layers are yours).
从您的表示层,将 DTO 视为合同,您将收到一个对象,您必须将其视为对您的应用程序陌生的对象,并且对其没有任何控制权(即使您拥有服务、dto 和表示层)是你的)。
Finally if you do this clean separation, developpers can work together with ease. The person who design ViewModels, Views and Controllers don't have to worry about the service layer or the DTO implementation because he will make the mapping when the others developpers finish their implementation... He can even use Mocking tool or manual mocking to fill the presentation layer with data for test.
最后,如果您进行这种干净的分离,开发人员可以轻松地协同工作。设计ViewModels、Views和Controllers的人不必担心服务层或DTO的实现,因为他会在其他开发人员完成他们的实现时进行映射......他甚至可以使用Mocking工具或手动mocking来填充具有测试数据的表示层。
回答by sgwill
For some simple views I'll use my DTO as my models, but as Views become more complex I'll create ViewModels.
对于一些简单的视图,我将使用我的 DTO 作为我的模型,但随着视图变得更加复杂,我将创建视图模型。
For me it is a balance between quickness (using DTO, since I already have 'em) and flexibility (creating ViewModels means more separation of concerns).
对我来说,这是速度(使用 DTO,因为我已经有了它们)和灵活性(创建 ViewModel 意味着更多的关注点分离)之间的平衡。
回答by Lalit Khanna
If you will use DTO as ViewModel, that means you are making high dependency on DTO because of some reason you are changing DTO then it could impact on ViewModel.
如果您将 DTO 用作 ViewModel,则意味着您高度依赖 DTO,因为某些原因您正在更改 DTO,那么它可能会影响 ViewModel。
Better use DTO & convert into viewmodel.
更好地使用 DTO 并转换为视图模型。
回答by Md. Saddam Hossain
We can use DTOsame as Model class and we can use viewmodel when we needs to show/use multiple models data/property in a single view. Example: I create some model using entity framework database first. So, now all the model generate based on the database. and now we need data annotation, for those data annotation we can create a folder name DTO, In this DTO folder, we can keep all the models exact which already generate and add data annotation above the property. Then we can use any operation(use controller , views) using this DTO classes. And when we need complex view, I mean when we need multiple classes data in one view there we can use viewmodel. For viewmodel we can create a folder name viewmodel, then create a custom class and keep that property which we need. I tried to clear myself. Any suggestion highly appreciated.
我们可以使用DTO与 Model 类相同,当我们需要在单个视图中显示/使用多个模型数据/属性时,我们可以使用 viewmodel。示例:我首先使用实体框架数据库创建了一些模型。所以,现在所有的模型都是基于数据库生成的。现在我们需要数据标注,对于那些数据标注,我们可以创建一个文件夹名称DTO,在这个DTO文件夹中,我们可以保留所有已经生成的模型,并在属性上方添加数据标注。然后我们可以使用这个 DTO 类使用任何操作(使用控制器,视图)。当我们需要复杂的视图时,我的意思是当我们在一个视图中需要多个类数据时,我们可以使用视图模型。对于viewmodel,我们可以创建一个文件夹名称viewmodel,然后创建一个自定义类并保留我们需要的那个属性。我试图清除自己。任何建议都非常感谢。

