Data Transfer Objects (DTOs) appear frequently in Java projects but are often abused or misunderstood.
They are typically used to move data between domains within an application or system. They are often part of a Domain Model, but don’t have to be. See my article “What is a Domain Object anyway?” for a little more detail on Domains.
A well-designed Data Transfer Object is immutable. This means that once the object is created, the values in it’s members cannot be changed.
To change a value within a DTO, you create a new DTO with the desired values.
This is an important difference between a Data Transfer Object and other Domain Objects. Normal domain objects are mutable. You can change their member values.
Data Transfer Objects contain NO business logic.
On rare occasions it makes sense to have data transformation logic in a DTO but usually code in a DTO is an indicator of a flaw in your application architecture.
So, in summary, if:
- You cannot change the values of your members (fields)
- You contain no business logic
- You are used to pass information between Domains
You are probably a Data Transfer Object.