How I use DTOs


Posted by Steven

Because I recently read about this pattern, here are some words about how I use DTOs, Data Transition Objects. DTOs are used to transport data, for example from server to client and vice versa. Let's have a look at the case where you want to load data from a server to represent it in a client. Often, the user interface represents data from different entities, like having an administration interface where persons and their permissions are shown. Because the entity person and the entity permission are involved, it's best to have an DTO that wrapps the needed data. So in this case, the DTO is kind of a summary of the needed information.

  1. public class Person {
  2.  
  3. private Long id;
  4. private String name;
  5.  
  6. // Dozer doesn't understand java.time.Instant ... too bad.
  7. private Date creationDate;
  8. private Date changeDate;
  9.  
  10. // getter / setter
  11. }

and

  1. public class Permission {
  2.  
  3. private Long id;
  4. private Person person;
  5.  
  6. private String text;
  7.  
  8. // getter / setter
  9. }

is transported as

  1. public class PersonDTO {
  2.  
  3. private Long id;
  4.  
  5. private String name;
  6.  
  7. private List<String> permissions;
  8.  
  9. // Dozer doesn't understand java.time.Instant ... too bad.
  10. private Date creationDate;
  11. private Date changeDate;
  12.  
  13. // getter / setter
  14. }

DTOs are also used for getting data from the client to the server so it can be persisted. I often see that people reuse their DTOs send from the server for getting data back there. I think it's better to use an UpdateDTO like this:

  1. public class PersonUpdateDTO {
  2.  
  3. private Long id;
  4.  
  5. private String name;
  6.  
  7. private List<String> permissions;
  8.  
  9. // getter / setter
  10. }

This looks pretty much like the DTO the data came in from the server, but it just contains the attributes that have to be send to the server. In my example, the DTO contains meta-information like when the person's permission was changed the last time. Because this attribute is set by the database, it doesn't need to be send from the client to the server. Often, these attributes are set to null which results in code like

  1. persistService.save(new PersonDTO("The Dude", null, null));

because the time of creation and update is set to null. Even worse is this:

  1. persistService.save(new PersonDTO("The Dude", new Date(), new Date()));

because the time set to this object will be overriden by the server and hence doesn't have any purpose. This is the solution I prefer:

  1. persistService.save(new PersonUpdateDTO("The Dude"));

I'm loading everything I need to display in one request, so there are a lot of attributes to set in a DTO. To avoid code like

  1. PersonDTO dto = new PersonDTO(person.getName(), person.getSurname(), person.getCreationDate(). person.getUpdateDate());

I use Dozer so the code looks like this:

  1. PersonDTO personDTO = new DozerBeanMapper().map(person, PersonDTO.class);

Recently I had the idea to rename our PersonDTO to PersonTO like in "Person Transfer Object" (note the missing "D") because for me it was clear that we want to transport data. However, DTO is a pattern by Martin Fowler. That is the reason why you want your DTOs to be DTOs. ;)

TL;DR:

Use DTOs instead of sending your entities. Use different objects for loading and writing: DTO and UpdateDTO. Use Dozer for mapping. Name them "DTO".

(Photo: https://en.wikipedia.org/wiki/Transport_in_Mali#/media/File:Mali_-_local_transport.jpg By Ferdinand Reus from Arnhem, Holland - Flickr, CC BY-SA 2.0, https://commons.wikimedia.org/w/index.php?curid=3249038)

Share: