Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command objects and DTOs, difference?

When we speak of command objects in Grails and even in Spring, are they the same as data transfer objects? Meaning, is a command object an example of the implementation of the DTO enterprise design pattern? If not, what is the difference?

like image 266
Anonymous Human Avatar asked Oct 28 '16 14:10

Anonymous Human


People also ask

What means DTOs?

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. DTOs are commonsense solutions for people with programming backgrounds.

Are DTOs value objects?

DTO is a class representing some data with no logic in it. On the other hand, Value Object is a full member of your domain model. It conforms to the same rules as Entity. The only difference between Value Object and Entity is that Value Object doesn't have its own identity.

When should DTOs be used?

The reason you want to use DTOs is that you want clients to couple to that contract, not to your internal data structures. This allows you to modify and evolve your internals freely without breaking clients.

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.


Video Answer


1 Answers

A data transfer object (DTO) is an object (simple java bean) that carries data between any two layers or processes. You might generally introduce/use DTO layer & populate the DTO bean with the data received from an external web service or external system. Refer Martin Fowler's blog on Data Transfer Object for more details

Command object is just a spring (mvc) terminology which maps the html form data to a java bean (form bean). Here Spring Dispatcher servlet & helper classes map the data from html form to the java bean. In Grails, command objects do serve more than data carriers like AST transformation.

In summary, they both are data carriers from one layer to the other.

Is a command object in Spring an example of the implementation of the DTO enterprise design pattern ?

Yes, but the point to note is that the Spring Dispatcher servlet & helper classes populate the command object with html form data.

like image 174
developer Avatar answered Sep 21 '22 12:09

developer