ASP.NET Core Web API Interview Question #22
What are DTOs in ASP.NET Core Web API?
Web API & REST Fundamentals, Controllers, HTTP Methods & Status Codes Junior Beginner
Quick Interview Answer
DTO stands for Data Transfer Object. A DTO defines the data contract exchanged between an API and its clients, such as CreateProductRequest, UpdateProductRequest, or ProductResponse.
Detailed Explanation
DTO stands for Data Transfer Object. A DTO defines the data contract exchanged between an API and its clients, such as CreateProductRequest, UpdateProductRequest, or ProductResponse.
DTOs help prevent persistence entities from becoming public API contracts. They also allow the API to control validation, hide internal fields, shape responses efficiently, and evolve the external contract more deliberately.
Code Example
public sealed class ProductResponse
{
public int Id { get; init; }
public string Name { get; init; } = "";
public decimal Price { get; init; }
}