数据传输对象与值对象相同吗?
数据传输对象与值对象相同还是不同?如果它们不同,那么我们应该在哪里使用 DTO,在哪里使用 VO?
我们所说的编程语言是Java,上下文是——有一个Web应用程序,它从数据库中获取数据,然后对其进行处理,最终将处理后的信息显示在前端。
Is a Data Transfer Object the same as a Value Object or are they different? If they are different then where should we use a DTO and where should we use a VO?
The programming language we are talking about is Java and the context is - there is a web application, which fetches data from a database and then processes it and ultimately the processed information is displayed on the front-end.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
值对象是一个简单的对象,其相等性不基于身份。
数据传输对象是用于在软件应用程序子系统之间(通常是业务层和UI之间)传输数据的对象。它只关注纯数据,因此没有任何行为。
A value object is a simple object whose equality isn't based on identity.
A data transfer object is an object used to transfer data between software application subsystems, usually between business layers and UI. It is focused just on plain data, so it doesn't have any behaviour.
数据传输对象是用于将一堆数据从一层或一层移动到另一层的拼凑物,目标是通过将一堆内容打包到同一数据结构中并将其一起发送来最大程度地减少来回调用的数量。有些人也使用它,例如Michael 在他的帖子中指出,这样一层使用的类就不会暴露到调用它的层。当我将 DTO 称为拼凑时,我的意思是没有实现精确的抽象概念,它是帮助应用程序层之间通信的实用解决方法。
值对象是我们只对其值感兴趣的东西,例如货币金额、日期范围,或来自 查找表。它没有身份,这意味着如果您有多个它们,您将不会担心跟踪哪个是哪个,因为它们本身并不是事物。
将值对象与系统中具有唯一标识的事物(称为实体)进行对比。如果您有一个跟踪客户付款的系统,则客户和付款都是实体,因为它们代表特定的事物,但付款的金额只是一个值,它本身并不存在,就您的系统而言。某事物与您的系统的关系决定了它是值对象还是实体。
A Data Transfer Object is a kludge for moving a bunch of data from one layer or tier to another, the goal is to minimize the number of calls back and forth by packing a bunch of stuff into the same data structure and sending it together. Some people also use it, like Michael points out in his post here, so that the classes used by one layer are not exposed to the layer calling it. When I refer to DTO as a kludge, I mean there's not a precise abstract concept getting implemented, it's a practical workaround for helping with communication between application layers.
A Value Object is something where we're only interested in its value, like a monetary amount, a date range, or a code from a lookup table. It does not have an identity, meaning you would not be concerned, if you had several of them, of keeping track of which is which, because they are not things in themselves.
Contrast Value Objects to things that do have a unique identity in your system, which are called Entities. If you have a system where it tracks a customer making a payment, the customer and the payment are entities, because they represent specific things, but the monetary amount on the payment is just a value, it doesn't have an existence by itself, as far as your system is concerned. How something relates to your system determines if it is a Value Object or an Entity.
如果您不想将实际的域对象发送到服务的客户端,请在服务的边界使用 DTO - 这有助于减少客户端和服务之间的依赖关系。
值对象只是其相等性不基于身份的对象,例如 java.lang.Integer
DTO 和值对象并不是真正的彼此替代品。
use a DTO at the boundary of your services if you don't want to send the actual domain object to the service's clients - this helps reduce dependencies between the client and service.
values objects are simply objects whose equality isn't based on identity e.g. java.lang.Integer
DTOs and value objects aren't really alternatives to each other.
它们是不同的,但我过去甚至交替使用这两者,这是错误的。我在《核心 J2EE 模式》一书的第一版中读到,DTO(数据传输对象)被称为 VO(值对象),但无法找到该参考资料。
DTO(我有时将其称为“哑传输对象”)是为了帮助我记住它是一个容器,不应该有任何业务逻辑,用于在层与层之间传输数据。它应该是一个具有 getter/setter 属性的对象。
然而,VO 类似于 JAVA Enum,代表一组固定的数据。 VO 没有对象标识(对象实例在内存中的地址),它由其值标识并且是不可变的。
They are different, but I've even used the two interchangeably in the past, which is wrong. I read that DTO (Data Transfer Object) was called a VO ( Value Object) in the first edition of the Core J2EE Patterns book, but wasn't able to find that reference.
A DTO, which I've sometimes called a Dumb Transfer Object to help me remember it's a container and shouldn't have any business logic is used to transport data between layers and tiers. It just should be an object with attributes that has getters/setters.
A VO however is similar to a JAVA Enum and represents a fixed set of data. A VO doesn't have object identity (the address of the object instance in memory), it is identified by its value and is immutable.
Martin Fowler,谈论数据传输对象 (DTO):
因此,术语“值对象”已被用来表示 DTO,但对于他(和其他发帖者)而言,似乎不鼓励将其用作 DTO。
Martin Fowler, talking about Data Transfer Objects (DTOs):
So the term "Value Object" has been used to mean DTO, but as of him (and the other posters), its use as a DTO seems discouraged.
Matthias Noback 文章中的详细答案 是吗DTO 还是值对象?
简而言之,DTO:
值对象:
Good detailed answer in Matthias Noback article Is it a DTO or a Value Object?
In short a DTO:
A value object:
也许是因为缺乏经验,但我会这样说:这是范围的问题。
DTO 具有字传输功能,因此这意味着系统的某些部分将使用它进行通信。
值对象的范围较小,您将把值对象中的数据集而不是数组中的数据从一个服务传递到另一个服务。
据我所知,它们都不是“平等性不基于身份的对象”。
Maybe because of lack of experience, but I would put it this way: It's the matter of scope.
DTO has word transfer in it so it means some parts of the system will communicate using it.
Value object has smaller scope, you will pass set of data in value object instead in array from one service to the other.
As much as I understood niether of them is "object whose equality isn't based on identity".