视图模型和数据传输对象之间的区别是什么?

时间:2021-04-03 16:34:54

I'm basing this question on Fowler PoEAA. Given your familiarity with this text, aren't the ViewModels used in ASP.NET MVC the same as DTOs? Why or why not? Thank you.

我的问题是基于福勒·波亚的。如果您熟悉这篇文章,那么在ASP中使用的ViewModels不是吗?NET MVC和DTOs一样吗?为什么或为什么不呢?谢谢你!

4 个解决方案

#1


75  

They serve a similar purpose (encapsulating data for another layer of the application) but they do it differently and for different reasons.

它们的用途类似(为应用程序的另一层封装数据),但它们的用途不同,原因也不同。

  • The purpose of a DTO is to reduce the number of calls between tiers of an application, especially when those calls are expensive (e.g. distributed systems). DTOs are almost always trivially serializable, and almost never contain any behaviour.

    DTO的目的是减少应用程序层之间的调用数量,特别是当这些调用非常昂贵时(例如分布式系统)。dto几乎总是可以序列化的,而且几乎从不包含任何行为。

    For example, you're developing an e-Commerce site. CreateCustomer and AddCustomerAddress are separate operations at the database level, but you might for performance reasons want to aggregate their data into a NewCustomerWithAddressDto so that your client only needs to make one round-trip to the server, and doesn't need to care that the server might be doing a bunch of different things with the parcel of data.

    例如,您正在开发一个电子商务网站。CreateCustomer和AddCustomerAddress是分开操作在数据库级别上,但你可能出于性能的考虑,想要将他们的数据聚合成NewCustomerWithAddressDto,这样你的客户只需要向服务器发出一个往返,和不需要护理,服务器可能会做很多不同的事情与包裹的数据。

  • The term "ViewModel" means slightly different things in different flavours of MV*, but its purpose is mainly separation of concerns. Your Model is frequently optimised for some purpose other than presentation, and it's the responsibility of the ViewModel to decouple your View from the Model's implementation details. Additionally, most MV* patterns advise making your Views as "dumb" as possible, and so the ViewModel sometimes takes responsibility for presentation logic.

    “ViewModel”一词指的是MV*不同味道中略有不同的东西,但其目的主要是关注点分离。除了表示之外,您的模型经常被优化,视图模型的职责是将视图与模型的实现细节分离开来。此外,大多数MV*模式都建议尽可能使视图“愚蠢”,因此ViewModel有时负责表示逻辑。

    For example, in the same e-Commerce application, your CustomerModel is the wrong "shape" for presentation on your "New Customer" View. For starters, your View has two form fields for your user to enter and confirm their password, and your CustomerModel doesn't contain a password field at all! Your NewCustomerViewModel will contain those fields and might, depending on your flavour of MV*, be responsible for some presentation logic (e.g. to show/hide parts of the view) and basic validation (e.g. ensuring that both password fields match).

    例如,在相同的电子商务应用程序中,您的CustomerModel在您的“新客户”视图上的表示是错误的“形状”。首先,您的视图有两个表单字段供用户输入和确认他们的密码,而且您的CustomerModel根本不包含密码字段!您的NewCustomerViewModel将包含这些字段,根据您的MV*风格,可能负责一些表示逻辑(例如显示/隐藏视图的部分)和基本验证(例如,确保两个密码字段匹配)。

#2


13  

DTOs in MVVM and MVP are usually Very Dumb Objects and are basically just a bunch of property setters and getters. ViewModels on the other hand can have some behaviour.

MVVM和MVP中的dto通常是非常愚蠢的对象,基本上就是一堆属性设置程序和getter。另一方面,视图模型可以有一些行为。

A practical positive side effect of having DTOs is allow easier serialization. If you have a rather complex object in, say C#, you will often find yourself having to selectively turn things off that you don't want serialized. This can get rather ugly and DTOs simplify this process.

具有dto的一个实际积极的副作用是允许更容易的序列化。如果你有一个相当复杂的对象,比如c#,你会经常发现自己不得不选择性地关闭你不想要序列化的东西。这会变得相当丑陋,dto简化了这个过程。

#3


12  

The purpose is different:

目的是不同的:

  • DTO's are used to transfer data
  • DTO用于传输数据
  • ViewModels are used to show data to an end user.
  • 视图模型用于向最终用户显示数据。

So normally ViewModels contain the presentation data, witch is in a lot of cases similar to what is in a DTO, but with some differences. Think of representation of enums, localization, currency, date formats, ... . This is because normally there should be no logic in your view.

通常viewmodel包含表示数据,在很多情况下,witch都和DTO很相似,但是有些不同。考虑枚举的表示、本地化、货币、日期格式等等。这是因为通常你的视图中不应该有任何逻辑。

#4


1  

A View Model and a Data Transfer object has similarities and differences.

视图模型和数据传输对象具有相似性和差异性。

Similar: Transfer data in a record (object instance, perhaps serialized) to a receiver, whether a view or a service

类似的:将记录中的数据(对象实例,可能是序列化的)传输到接收方,无论是视图还是服务

Difference: A View Model is intended to be sent to a View, where it will be displayed, with formatting. A View Model also sends back data to a controller. A DTO is usually not intended for presentation. It is intended to send raw data.

差异:视图模型将被发送到视图中,并显示在视图中,并进行格式化。视图模型还向控制器发送数据。DTO通常不是用来表示的。它旨在发送原始数据。

#1


75  

They serve a similar purpose (encapsulating data for another layer of the application) but they do it differently and for different reasons.

它们的用途类似(为应用程序的另一层封装数据),但它们的用途不同,原因也不同。

  • The purpose of a DTO is to reduce the number of calls between tiers of an application, especially when those calls are expensive (e.g. distributed systems). DTOs are almost always trivially serializable, and almost never contain any behaviour.

    DTO的目的是减少应用程序层之间的调用数量,特别是当这些调用非常昂贵时(例如分布式系统)。dto几乎总是可以序列化的,而且几乎从不包含任何行为。

    For example, you're developing an e-Commerce site. CreateCustomer and AddCustomerAddress are separate operations at the database level, but you might for performance reasons want to aggregate their data into a NewCustomerWithAddressDto so that your client only needs to make one round-trip to the server, and doesn't need to care that the server might be doing a bunch of different things with the parcel of data.

    例如,您正在开发一个电子商务网站。CreateCustomer和AddCustomerAddress是分开操作在数据库级别上,但你可能出于性能的考虑,想要将他们的数据聚合成NewCustomerWithAddressDto,这样你的客户只需要向服务器发出一个往返,和不需要护理,服务器可能会做很多不同的事情与包裹的数据。

  • The term "ViewModel" means slightly different things in different flavours of MV*, but its purpose is mainly separation of concerns. Your Model is frequently optimised for some purpose other than presentation, and it's the responsibility of the ViewModel to decouple your View from the Model's implementation details. Additionally, most MV* patterns advise making your Views as "dumb" as possible, and so the ViewModel sometimes takes responsibility for presentation logic.

    “ViewModel”一词指的是MV*不同味道中略有不同的东西,但其目的主要是关注点分离。除了表示之外,您的模型经常被优化,视图模型的职责是将视图与模型的实现细节分离开来。此外,大多数MV*模式都建议尽可能使视图“愚蠢”,因此ViewModel有时负责表示逻辑。

    For example, in the same e-Commerce application, your CustomerModel is the wrong "shape" for presentation on your "New Customer" View. For starters, your View has two form fields for your user to enter and confirm their password, and your CustomerModel doesn't contain a password field at all! Your NewCustomerViewModel will contain those fields and might, depending on your flavour of MV*, be responsible for some presentation logic (e.g. to show/hide parts of the view) and basic validation (e.g. ensuring that both password fields match).

    例如,在相同的电子商务应用程序中,您的CustomerModel在您的“新客户”视图上的表示是错误的“形状”。首先,您的视图有两个表单字段供用户输入和确认他们的密码,而且您的CustomerModel根本不包含密码字段!您的NewCustomerViewModel将包含这些字段,根据您的MV*风格,可能负责一些表示逻辑(例如显示/隐藏视图的部分)和基本验证(例如,确保两个密码字段匹配)。

#2


13  

DTOs in MVVM and MVP are usually Very Dumb Objects and are basically just a bunch of property setters and getters. ViewModels on the other hand can have some behaviour.

MVVM和MVP中的dto通常是非常愚蠢的对象,基本上就是一堆属性设置程序和getter。另一方面,视图模型可以有一些行为。

A practical positive side effect of having DTOs is allow easier serialization. If you have a rather complex object in, say C#, you will often find yourself having to selectively turn things off that you don't want serialized. This can get rather ugly and DTOs simplify this process.

具有dto的一个实际积极的副作用是允许更容易的序列化。如果你有一个相当复杂的对象,比如c#,你会经常发现自己不得不选择性地关闭你不想要序列化的东西。这会变得相当丑陋,dto简化了这个过程。

#3


12  

The purpose is different:

目的是不同的:

  • DTO's are used to transfer data
  • DTO用于传输数据
  • ViewModels are used to show data to an end user.
  • 视图模型用于向最终用户显示数据。

So normally ViewModels contain the presentation data, witch is in a lot of cases similar to what is in a DTO, but with some differences. Think of representation of enums, localization, currency, date formats, ... . This is because normally there should be no logic in your view.

通常viewmodel包含表示数据,在很多情况下,witch都和DTO很相似,但是有些不同。考虑枚举的表示、本地化、货币、日期格式等等。这是因为通常你的视图中不应该有任何逻辑。

#4


1  

A View Model and a Data Transfer object has similarities and differences.

视图模型和数据传输对象具有相似性和差异性。

Similar: Transfer data in a record (object instance, perhaps serialized) to a receiver, whether a view or a service

类似的:将记录中的数据(对象实例,可能是序列化的)传输到接收方,无论是视图还是服务

Difference: A View Model is intended to be sent to a View, where it will be displayed, with formatting. A View Model also sends back data to a controller. A DTO is usually not intended for presentation. It is intended to send raw data.

差异:视图模型将被发送到视图中,并显示在视图中,并进行格式化。视图模型还向控制器发送数据。DTO通常不是用来表示的。它旨在发送原始数据。