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.

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).


