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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With