What is DTO(Data Transfer Object) and why you should use it?

The basic idea of using DTOs is to separate data from business logic and app-specific functionality. This is especially interesting in cases you need to pass data to a different layer (e.g. from Model layer to View layer) or receive data from a different service (external API, microservice etc.

After all, a DTO is just a simple data structure that holds various properties of built-in datatypes (or other DTOs). The advantage of enforcing this restriction comes in the form of interchangeability between systems and serializability.

For example, when we query a database table Students, we don’t want to expose all data columns in the database, but only send the minimum amount of information to the consumer, therefore we define a DTO (StudentDto) to represent the desired data structure, and convert Student entities into StudentDto objects. A typical implementation is like the code below.

Image for post
A DTO class (left) and its usage (right)

We can see from the code that the definition for the StudentDto class only contains limited data properties and has no behavior method. The StudentDto class is merely being used as a “dumb bag” for information transferred from the database to its recipients.

Why using it?

  • Internals – If you’re not using DTOs, then you’re likely exposing internal data structures.
  • Coupling – The reason you want to use DTOs is that you want clients to couple to that contract(it’s a data structure that is a contract between the producer and the consumer), not to your internal data structures. This allows you to modify and evolve your internals freely without breaking clients.
  • Representation – When creating an HTTP API, it’s all about representations. Most often times clients need a rich representation of a resource, not a just serialized version of a database entity. They often times need related data.Having your API return rich representations means you must do some level of composition to create an object, not just a database entity, that will get serialized. This is where a DTO comes into play.
  • Contracts – The moment you want change internal data objects, you now have to update the clients.

When to use a DTO?

As mentioned earlier, DTOs should be used every time data moves between layers and systems. So for my personal taste, everything that is more than a (key, value) pair and moving between layers should be a DTO. Like, don’t be afraid of having too many DTOs. Most of the time you probably don’t have enough!

When not to use DTO?

Obviously it can be an overkill to define a DTO that only stores one property (even though there are cases where understandability can be improved by doing this).

Disclaimer

Please note that this is a personal blog. The opinions expressed here represent my own and not those of my current or past employer, clients, government agencies and/or other people. All data and information is provided on this site is for informational purposes only and on an as-is basis. This site makes no representations as to accuracy, completeness, timeliness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. Furthermore, my Blog editor (currently Blogger) does not have any provisions for automatic spell checking that I can find. Thus, please don’t complain about spelling or grammar (especially if I’m sending this from my BlackBerry). Additionally, my thoughts and opinions change constantly. This is part of having an open and ever curious mind. This blog is intended to provide a brief snapshot and manifestation of the various random thoughts I’ve had throughout the day, and as such thoughts and opinions expressed within out-of-date posts may not be the same, nor even similar, to those I may hold today. Therefore, some entries, I will update from time-to-time. This may seem revisionist, but I don’t always have time to reflect and elaborate at that particular moment and/or may want to clarify them in the future. Blogs with titles are entries I have made for this blog site and this site only. Any others are captured from select Twitter or Facebook feeds to maintain a timeline or to remind me of an event.