DTO
A plain object whose only job is to carry data across a boundary, such as the body of an API request or response.
In more detail
A DTO has fields and no behaviour, and its shape is the contract between client and server. Keeping DTOs separate from internal database models lets each change independently.
Examples
C# records CreateProductRequest and ProductResponse in the Catalog APItype OrderCheckoutDTO = z.infer<typeof OrderCheckoutSchema>type UserDTO = z.infer<typeof UserSchema> keeps the type and the validation rules identical.Related terms
- record — A C# type declared with the record keyword for data, for which the compiler generates a constructor, properties, value equality, a readable ToString and with-expression copying.
- entity — A thing in an application's domain with its own identity, such as a user, account or product, usually stored in a database.
- object mapping — Copying data from one type to another across a boundary, such as from a database entity to an API response record.
- API contract — The written promise an API makes about its operations, inputs, outputs and errors, which callers rely on while the code behind it changes.
- validation schema — A description of what valid data looks like, checked by code while the program runs so bad data is rejected at the door.
- type inference — The compiler working out a type from the code itself, so you do not have to write it out by hand.
- payload — The actual data a message carries, as opposed to the headers and framing around it.
- utility types — Generic types built into TypeScript, such as Partial, Pick and ReturnType, that compute a new type from an existing one.
- schema drift — Copies of the same data shape slowly becoming different as one is changed and the others are forgotten.
- CRUD — Short for create, read, update and delete, the four basic things you can do to stored data.
Taught in
- Records, Pattern Matching, and Modern C# — Modern .NET Core, C# & Enterprise Microservices
- Manual Object Mapping — Modern .NET Core, C# & Enterprise Microservices
- End-to-End Type Safety: Shared Schemas & DTOs — Production Full-Stack Engineering
- Core Utilities: Partial, Required, Readonly, Pick And Omit — Production TypeScript
- Union Utilities: Exclude, Extract, NonNullable And Awaited — Production TypeScript
- Mapped Types, Key Remapping With as And Template Literals — Production TypeScript