views:

99

answers:

2

Hello,

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. As such may not map directly on to a domain object, i.e. may include data from one or more domain objects.

  1. Client side
  2. May contain business logic, e.g. validation, calculated properties, etc
  3. No persistence related methods

Domain object:

Object that models a real world object in the problem domain like Reservation, Customer, ORder, etc. Used to persists 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. A DTO contains just data and accessor methods, no logic. The data is for a particular DB transaction so may not may directly on to a domain object, i.e. may include data from one or more domain objects.

  1. Used on both sides as passed between layers
  2. No business logic
  3. No persistence related methods

So to the questions:

(1) Is my 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?

Thanks.

Tim

+1  A: 

The Domain and DTO can also be your "model" objects - you can have a view to render the details of the "Customer" domain object.

A domain object can have business logic to enforce the properties of the domain entity. validation is one such case. The domain object by itself does not contain persistence related methods, but it can have meta-data (like annotations) to support persistence

the POJO programming model makes it possible to use the same object as your domain, DTO and model objects - essentially, you will not be implemented any extraneous interfaces that will only apply to one layer but does not apply to others.

kartheek
Yes this is what I am doing. Indeed in nearly every case I have never had a need to use anything other than the Domain object. DTO would be for a complex query with multiple data items spanning domain objects.
Timothy Mowlem
And a separate MVC Model class is only really necessary if there is significant business logic/processing associated with the model data to be displayed?
Timothy Mowlem
yes, that will be one reason to have a proper dedicated model as opposed to using a domain object. Your domain object might be storing the date only UTC and that's enough for all your biz logic also. But on the UI, lets say you will have to display it in the user account's timezone. A model will be useful to for doing this UI specific computations.
kartheek
A: 

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.

ProfK
Thank you to both the responders. It seems more clear to me now. Domain objects can have business logic like validation, logic related directly to the data.
Timothy Mowlem
A separate MVC Model object is only necessary to encapsulate logic related to the displaying the data in the view. If there isn't any then it is easier to use the domain object as the MVC Model object.
Timothy Mowlem