How to know if you are a Data Transfer Object

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.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s