20

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.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Gaurav
  • 1,570
  • 4
  • 20
  • 25
  • 2
    [An object that carries data between processes in order to reduce the number of method calls.](http://martinfowler.com/eaaCatalog/dataTransferObject.html) – Abhijeet Jul 19 '16 at 09:43

7 Answers7

16

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.

JuanZe
  • 8,007
  • 44
  • 58
  • 2
    Can you please elaborate on "whose equality isn't based on identity", giving some example? – Gaurav Jan 22 '12 at 15:49
  • 3
    I could imagine that this sentence would mean that a VO does not have to map directly against a domain entity, rather than to some fields of it or a diferent "picture" of it. Usually a VO is understood to be the same as a DTO, but DTO bases more on "field composition" (some fields from diferent domain classes). A DTO example could be a composition between fields from a CustomerAddress class and a Customer class to serve it to the presentation layer. – frandevel Apr 09 '13 at 07:00
  • 2
    @Gaurav two value objects are equal when they have the same values, but not necessarily being the same object... – JuanZe Apr 09 '13 at 15:54
9

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.

Community
  • 1
  • 1
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • "How something relates to your system determines if it is a Value Object or an Entity." -> That said I would expect in Federal Banking System it can be that Money (a paper note) is an Entity as it has it's serial number as identity. – Vladimir Vukanac Oct 09 '22 at 10:33
6

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.

Michael
  • 210
  • 2
  • 7
3

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.

James Drinkard
  • 15,342
  • 16
  • 114
  • 137
  • Is it expected to DTO to do validation or it is allowed to pass "invalid/unsupported" data to the service? – Vladimir Vukanac Oct 09 '22 at 10:36
  • That is another question, but the short answer is no, in my opinion. The checks on the data should be before the data loads or after it reaches the destination, but before it's displayed or both. The answer depends on the type of data and how you are validating it. – James Drinkard Oct 10 '22 at 12:49
1

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.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
0

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
Alex
  • 21
  • 4
0

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