IT序号网

Java 中的 Grails 项目 DTO

lhb25 2025年12月25日 编程语言 35 0

在 Grails 中,有一个插件 compile ":dto:0.2.4" 用于将 Domain 对象传输到 DTO。使用该插件时,DTO 将创建为 Java 类。

举个例子,如果有像 Person.groovy 这样的域类,那么 DTO 就会像 PersonDTO.java 那样创建

这种行为的意图是什么?任何评论将不胜感激。

请您参考如下方法:

Peter Ledbrook 在 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.

所以,基本上它可以是域类的轻量级版本,仅包含客户需要的数据。

Grails 的情况并非如此,它具有数据库查询的静态方法,但如果您有 DAO类中,DTO 模式可用于确保不允许您的客户端执行接触数据库的方法。这可以很好地确保在表示层中不当使用此对象。


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!