ASP.NET Core Interview Question #121
What is the difference between ViewModel, DTO, and domain/entity model?
Routing, Controllers & Model Binding Mid-Level Intermediate
Quick Interview Answer
A ViewModel is shaped for a UI view and may combine display data, input fields, selections, or presentation-specific properties.
Detailed Explanation
A ViewModel is shaped for a UI view and may combine display data, input fields, selections, or presentation-specific properties.
A DTO represents data transferred across a boundary such as an API request or response. It should express the contract needed by that boundary.
A domain model represents business concepts and behavior, while an EF Core entity represents persisted data and relationships. In small applications these roles can overlap, but keeping external input/output models separate from persistence entities improves security, maintainability, and API evolution.
Code Example
public class ProductEditViewModel
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
public IEnumerable<SelectListItem> Categories { get; set; } = [];
}