The Data Transfer Object design pattern
The Data Transfer Object (DTO) pattern is the equivalent of the View Model pattern, but for web APIs. Instead of targeting a view, we are targeting the consumers of a web API endpoint.
Goal
The goal is to limit and control the inputs and outputs of an endpoint to the data that we need, decoupling the API's contract from the application's inner workings. DTOs should empower us to define our APIs without thinking about the underlying data structures, leaving us the choice to craft our web services the way we want. More precisely, we can craft them the way we want the consumers to interact with them. So no matter the underlying system, we can use DTOs to design endpoints that are easy to consume and maintain. Another possible outcome would be to save bandwidth by limiting the amount of information that the API transits.
Design
Let's start by analyzing a schema, which you may find similar to the one we saw when visiting...