Search code examples
grailsgroovygrails-plugin

Grails Project DTOs in Java


In Grails there is a plug-in compile ":dto:0.2.4" to transfer Domain objects to DTOs. When using that plug-in the DTOs are created as Java classes.

For an example if there is Domain Class like Person.groovy the DTO is created like PersonDTO.java

What is the intention of this kind of a behavior ? Any comment would be appreciated.


Solution

  • Peter Ledbrook answer your question in this blog post.

    Despite that, DTOs still persist (pardon the pun). When you want to serialise data over RPC, they’re often one of the few options available to you. GWT-RPC is a case in point, and the reason for the Grails DTO plugin. Gilead allows you to transparently serialise Hibernate domain instances, but this only works if the domain class can be loaded by the client. Since GORM domain classes are typically Groovy, that’s not an option with GWT. Your typical Grails domain class also includes a bunch of stuff that the client is hardly going to be interested in, like the custom mappings.

    So, basically it can be an lightweight version of your domain class, only with the data that your client needs.

    Not the case of Grails, that have static methods to database query's, but if you have a DAO class, the DTO pattern can be used to ensure that your client will not be allowed to perform the methods that touch the database. This can be good to ensure inappropriate use of this objects in your presentation layer.