DEV Community

Cover image for DTOs, ViewModel and Domain Model in C#
Shreyans Padmani
Shreyans Padmani

Posted on

DTOs, ViewModel and Domain Model in C#

DTOs, ViewModels, and Domain Models each play a unique role in clean and maintainable architecture.
They help separate business logic, UI logic, and data transfer responsibilities for better structure.
Using them correctly leads to scalable, testable, and professional applications.

What is Domain Models ?

  • The Domain is the business area or problem space your application is built to solve.
  • It represents the real-world rules, processes, and logic of the business.
  • Everything in the Domain layer focuses on business behavior, not on UI, databases, or APIs.
  • It includes Entities, Value Objects, Aggregates, Services, and Domain Logic.

How a Domain Works

  • The Domain defines how the business should work, independent of technical layers.
  • Domain models hold real business rules (e.g., calculating invoice total, validating stock, updating project status).
  • The Domain layer interacts with other layers through interfaces (Repository, Services), but stays independent.
  • The Application/UI layer calls the Domain to perform operations; the Domain decides how things should happen.
  • Changes in business rules affect only the Domain, keeping the rest of the system stable.

What Is a ViewModel?

  • A ViewModel is a class created specifically for the View (UI).
  • It contains only the data needed for the screen, not the entire database model.
  • It can combine multiple models (e.g., Project + Attachments + Dropdowns).
  • It often includes extra UI fields such as dropdown lists, checkboxes, or formatted display strings.

How a ViewModel Works

  • Controller fills the ViewModel and sends it to the View.
  • The View displays data from the ViewModel and sends user input back using the same ViewModel.
  • ViewModel helps remove direct dependency of your View on your Domain or Database Models.
  • It keeps the UI clean by providing exact properties required (e.g., SelectedStateId, FileUploadList).
  • In complex pages, ViewModels prevent the need for multiple queries by combining data into one object.

What Is a DTO (Data Transfer Object)?

  • A DTO is a lightweight class used to transfer data between layers (Controller → Repository → Service → API).
  • It contains only data, no business logic, no UI logic, no validation logic.
  • DTOs help avoid exposing Domain Models directly outside the application.

How a DTO Works

  • Controller receives/send data using DTOs instead of Domain Models.
  • Repository or Service maps data between DTO and Domain Model (using AutoMapper or manual mapping).
  • DTO protects sensitive or unnecessary fields from being exposed in API or View.
  • It improves performance by sending only required fields, not entire objects.

Conclusion

DTOs, ViewModels, and Domain Models each serve a unique purpose in a clean architecture.
They separate data transfer, UI presentation, and core business logic into clear layers.
Using them correctly improves maintainability, scalability, and code readability.
Together, they create a structured, professional, and easily extendable application design.

Top comments (0)