Search code examples
springspring-mvcgrails

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?


Solution

  • 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.