Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between an MVC Model object, a domain object and a DTO

What is the difference between a MVC Model object, a domain object and a DTO?

My understanding is:

MVC Model object:

Models the data to be displayed by a corresponding view. It may not map directly to a domain object, i.e. may include data from one or more domain objects.

  1. Client side
  2. May contain business logic. Eg. validations, calculated properties, etc
  3. No persistence related methods

Domain object:

An object that models real-world object in the problem domain like Reservation, Customer, Order, etc. Used to persist data.

  1. Server side
  2. No business logic

DTO (Data Transfer Object):

An object used to transfer data between layers when the layers are in separate processes, e.g. from a DB to a client app. Allows a single transaction across the wire rather than multiple calls when fetching data corresponding to multiple domain objects. A DTO contains just data and accessor methods and there is no logic present. The data is for a particular DB transaction, so it may or may not directly map to a domain object as it may include data from one or more domain objects.

  1. Used on both server and client sides as it is passed between layers
  2. No business logic
  3. No persistence related methods

So, the questions:

  1. Is above understanding correct? Am I missing some key points?

  2. Are there any reasons not to use Domain objects as the MVC Model assuming that the Model objects do not require extra business logic?

  3. Are there any reasons not to use DTOs as the MVC Model assuming that the Model objects do not require extra business logic?

like image 975
Timothy Mowlem Avatar asked Oct 04 '10 08:10

Timothy Mowlem


People also ask

What is the difference between DTO and Domain object?

If using anemic data model (i.e. your domain objects don't have any logic), DTO and domain object can be the same object. No. Domain objects have no specific relation to any persistence. In simple words, they are parts to ensure the business logic required to run the application.

Is DTO a model in MVC?

MVC Model Object and Domain Object both are same. 3) If there is no business logic in Domain Object then automatically it becomes DTO.

What is the difference between domain and model?

A domain model is a representation of the organization's data, independent of the way the data is stored in the database, with a domain being the collection of all the objects in that system, while the data model is used in database design and development.

What is a DTO MVC?

With MVC data transfer objects are often used to map domain models to simpler objects that will ultimately get displayed by the view. From Wikipedia: Data transfer object (DTO), formerly known as value objects or VO, is a design pattern used to transfer data between software application subsystems.


1 Answers

Domain and model objects are essentially the same, and may contain business logic. Depending on implementation, domain and DTO objects may be equivalent if you remove business logic from the model into a service class.

Often a key variant of the DTO is the View Model, which is used purely to transfer data between the domain model and the view, although often a View Model may contain logic, although this should be purely UI logic.

like image 108
ProfK Avatar answered Sep 18 '22 17:09

ProfK