Prev Next

# What is a Data Transfer Object (DTO)?

Whenever two parts of a software system want to communicate they need to exchange data. Data Transfer Objects (DTOs) are all about how this data is represented. If you know C then a DTO is a struct. If you don’t know C then a DTO is an object without methods, it is pure data.

Data Transfer Objects have only public fields, and these fields may have one of a limited set of types. This limits the risk of unnecessary coupling between modules, allows DTOs to be easily serialized (even though they are not java.io.Serializable), and makes DTOs easy to transform using the OSGi Converter. DTOs are therefore excellent candidates for service API parameters and return values, they can be used remotely, or outside of Java, and can be represented using JSON, YAML or any format of your choice.

## A DTOs Structure

Data Transfer Objects are public (static) classes with no methods, other than the compiler supplied default constructor, having only public fields limited to the easily serializable types: i.e. A DTO is equivalent to a struct in C. It is suggested (although not required) that a DTO type extend the DTO base class.

For example:

    public class BundleDTO extends DTO {
public long   id;
public long   lastModified;
public int    state;
public String symbolicName;
public String version;
}


The Types that can be used in DTOs is limited to:

• Primitive types
• Wrapper classes for the primitive types
• String
• enum
• Version
• Data Transfer Objects
• List
• Set
• Map
• array

The List, Set, Map and array aggregates must only hold objects of the listed types.

The object graph from a Data Transfer Object must be a tree (i.e. not have any cycles) to simplify serialization and deserialization. The DTO class also has some other specific requirements:

• public – Only public classes with public members work as DTO.
• static – Fields must not be static but inner classes must be static.
• no-arg constructor – To allow instantiation before setting the fields.
• extend – DTOs can extend another DTO.
• no generics – DTOs should never have a variable generic signature because this makes serialization really hard.

### Equals and HashCode

The DTO class has no equals nor a hashCode method. This makes them unsuitable as keys or for use in sets. The reason is that it is not the data (the DTO) that defines what is the primary key of the data, it is the actual usage scenario that defines the fields of interest. That is, one object can use the id field of the object as primary key but another object tracks by symbolicName and version.

You will therefore often find that DTOs are the values in maps but never a key. One field is then the key.

It is rare but allowed to add equals and hashCode methods.

The use of DTOs is introduced in the Microservies Tutorial via their use as Person and Address data objects.