Is a Data Transfer Object the same as a Value Object?

13.1k views Asked by At

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.

7

There are 7 answers

3
JuanZe On BEST ANSWER

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.

0
Michael On

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.

0
Alex On

Good detailed answer in Matthias Noback article Is it a DTO or a Value Object?
In short a DTO:

Declares and enforces a schema for data: names and types
Offers no guarantees about correctness of values

A value object:

Wraps one or more values or value objects
Provides evidence of the correctness of these values
2
James-Jesse Drinkard On

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.

0
serv-inc On

Martin Fowler, talking about Data Transfer Objects (DTOs):

Many people in the Sun community use the term "Value Object" for this pattern. I use it to mean something else.

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.

0
Zeljko Miloradovic On

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".

1
Nathan Hughes On

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.