liveBook · Manning

Data transfer object

As explained in Building Web APIs with ASP.NET Core

A data-transfer object is an object that packages data for transfer between software components or across an interface.

A data-transfer object (DTO) is a class shaped for information crossing an API boundary. It can carry the data a client needs without making a database entity double as the API’s request or response format. A simple weather-forecast DTO, for example, lets a controller serve strongly typed JSON. section 5.5.1 section 2.4

For a board-game response, create a /DTO/ folder and two classes. LinkDTO holds a link’s address, relationship, and HTTP method. RestDTO<T> holds Data of type T alongside a list of Links. The T is a placeholder: a controller can return RestDTO<BoardGame[]> when its data is an array of board games. This replaces a quick anonymous response with a consistent shape that can tell clients where to go next. section 3.1.6 section 3.5

Listing 3.3 RestDTO.cs file
namespace MyBGList.DTO
{
   public class RestDTO<T>
   {
       public List<LinkDTO> Links { get; set; } = new List<LinkDTO>();

       public T Data { get; set; } = default!;
   }
}

RestDTO<T> shows the reusable response shape: typed Data and an initialized list of LinkDTO objects. section 3.1.6

Updating a board game

Create BoardGameDTO with a required Id to find the record and optional Name and Year values to change. Accept it as the POST action’s input, find the entity, apply supplied changes, update its modification date, and save. The DTO describes what the client may send; the entity remains the database-facing class. In this implementation, the action returns a RestDTO containing the resulting BoardGame entity, so introducing an input DTO does not by itself separate every response from entities. section 5.5.1

Keeping DTOs separate matters when entities have fields clients should neither see nor transmit, such as password hashes or personal data. Define the fields for each exchange rather than relying on an entity’s full set of properties. section 5.5.1

Registering an account

RegisterDTO accepts a username, email address, and password. Required-field and email-address attributes describe input requirements; the registration action checks ModelState before creating a user. A malformed email demonstrates DTO validation, while password rules can still reject an otherwise valid request later. section 9.2.6

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Builds generic response and endpoint-specific input classes, then follows their use through controller actions and validation.
Examples
Uses a linked board-game response, a board-game update request, and account registration and login DTOs.
What it assumes
Introduces generic types while assuming basic database manipulation operations for the CRUD work; requires no specific SQL knowledge.
What you can do after
Build it from scratch for ASP.NET Core endpoints that accept selected inputs or return typed data with descriptive links.
How it connects
Connects DTOs to JSON, REST uniform interfaces, hypermedia links, database entities, CRUD operations, and model validation.

Also explained in

Compare how 6 books explain data transfer object →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.