Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Transfer Objects, Domain Objects and Repositories

I'm trying to figure out how all these work together. I know that a DTO is basically just a container of data for the Domain Objects to pass back and forth to forms and such. Does the Domain object contain a DTO or do the DTO and the Domain Object happen to just have all of the same properties that will be mapped manually?

If I am exposing my DTO type in a service, how do I use the getters and setters without creating a round trip for each get/set operation on the client? I know that you can have one long constructor, but that can get ugly if you have more than 7 properties.

When implementing the Repository pattern, do I pass in the DTO or the Domain Object?

like image 951
Charles Graham Avatar asked Nov 08 '08 07:11

Charles Graham


People also ask

What are DTO and DAO?

DTO — Data Transfer Object. DAO — Data Access Object.

What is the difference between DTO and domain object?

DTOs are mostly used out of the hexagon, in the delivery mechanism. On the other hand domain models should promote a good object oriented design with proper encapsulation. They belong in the hexagon. However it's not always clear where to use a DTO and a domain model, where and how to convert one into the other.

Can we use DTO in repository?

A typical example is a DTO projection, which is the most efficient one for read-only operations. To use it in a derived or custom JPQL query, you only need to change the return type of your repository method to your DTO class or interface.

What is the meaning of data transfer objects?

A data transfer object (DTO) is an object that carries data between processes. You can use this technique to facilitate communication between two systems (like an API and your server) without potentially exposing sensitive information.


2 Answers

  • The DTO's and the Domain objects should be separate.
  • There should be a mapper that maps a DTO to a domain object and a domain object to a DTO. This mapper should be an implementation of an interface, with the default mapper using reflection to map the objects to each other.
  • The repository should be a service that returns the domain objects, which themselves should services.
  • If the DTO is a class that is exposed by a web service, the WSDL that is created defines the property as an element, and the proxy that gets created on the other side just creates a getter / setter property that is run on the client itself, so the getters and setters do not cause a roundtrip.
  • Even if you just create a public variable in your DTO, the proxy will be implemented as a getter and setter.
like image 116
Charles Graham Avatar answered Oct 22 '22 13:10

Charles Graham


I think it's better to have the DTO contain a reference to the Domain object so that the DTO's consumers can begin using the Domain object. That said, if the DTO's consumers must not mutate the Domain object, you may need to have the DTO contain the values encapsulated in the Domain object. This can be difficult since you may need to do a deep copy of the Domain object.

I'm not sure why it's a problem that exposing a DTO type as a service would cause use of its getters/setters to do a round trip. If the service is a remote service, the returned DTO is serialized anyway and your getters/setters will get the copy of the values. If the service is not remote, it doesn't seem to be much of penalty to do a "round trip" since the client and the service are in the same process space.

like image 20
Alan Avatar answered Oct 22 '22 11:10

Alan