Using MVC with DTOs
This section explores leveraging the DTO pattern with the MVC framework.
This section is the same as we explored in Chapter 5, Minimal APIs, but in the context of MVC. Moreover, the two code projects are part of the same Visual Studio solution for convenience, allowing you to compare the two implementations.
Goal
As a reminder, DTOs aim to control the inputs and outputs of an endpoint by decoupling the API contract from the application’s inner workings. DTOs empower us to define our APIs without thinking about the underlying data structures, leaving us to craft our REST APIs how we want.
We discuss REST APIs and DTOs in more depth in Chapter 4, REST APIs.
Other possible objectives are to save bandwidth by limiting the amount of information the API transmits, flattening the data structure, or adding API features that cross multiple entities.
Design
Let’s start by analyzing a diagram that expands MVC...