Grails:当我不想保存任何内容时,我可以使用域对象吗? [英] Grails: Can I use domain objects when I don't want to save anything?

查看:121
本文介绍了Grails:当我不想保存任何内容时,我可以使用域对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些我的领域类越来越丰富:它们实现了一个有趣的可比较的,可能有正,负,乘和除,许多有一些方便的getter调用服务并确定复杂的事情。最重要的是,他们拥有合适的属性。我正在使用这些用于正常的数据库事务,并且在我只想要一个包含所有这些方法但可能不想保存它的对象的时候。

我的队友确信这是非常糟糕的,并且建议我应该使用DTO(数据传输对象),据我所知,它将是一个POGO / POJO与一个领域的类代码复制/粘贴。这似乎真的不干,我看不到的好处。将域对象不时用作常规对象有什么问题吗?我错过了DTO的要点吗?解析方案

Grails域类有些错误,因为应用程序的域层通常由持久性和非持久性类。但Grails域类始终是持久的。您可以拥有非持久性域(在传统意义上),但它们必须位于src / groovy或src / java中。这可能令人沮丧,因为然后域应用程序分成两个地方。我们已经请求了非持久性域类,例如像 static static = false 或类似的东西,但它还没有实现。



我想如果你想利用领域类的非持久性特征(例如验证,依赖注入等),那么可以有一些可以由数据库支持但不支持的类。你只需要在代码中记录它或者有某种约定,例如一个特殊的封装结构或命名约定。如果您从不调用GORM方法(如 save() list() findAllByFoo() 等等,那么就不会有任何数据库访问。



就DTO而言,它们可以是非DRY,但有一个帮助插件 - 请参阅 http://grails.org/plugin/dto 。它有一段时间没有更新,但我很确定它仍然有效。它有一个很好的功能,它将使用语法 domainObj作为DTO 从持久性域类实例创建DTO实例。您需要保持类之间的同步更改,但最初的DTO生成是通过脚本自动完成的。


Some of my domain classes are getting rather rich: They implement an interesting comparable, might have plus, minus, multiply and div, many have some convenient getters which call services and determine complicated things. And most of all, they have the right properties. I am using these both for the normal "database transactions" and also in times when I just want an object that has all those methods but may not want to save it.

My teammates are convinced that this is very bad, and advise that I should be using DTO (Data Transfer Objects) which, as I understand, would be a POGO/POJO with one of the domain class' code copy/pasted in. This seems really un-dry, and I can't see the benefit. Is there something wrong with using domain objects as regular objects from time to time? Am I missing the point of DTO's?

解决方案

Grails domain classes are somewhat misnamed, since an application's domain tier is usually made up of persistent and non-persistent classes. But Grails domain classes are always persistent. You can have non-persistent domain (in the traditional sense) classes but they would have to be in src/groovy or src/java. That can be frustrating because then the domain tier is split in the application into two places. We've had requests for non-persistent domain classes, e.g. something like static persistent = false or something similar, but it's not implemented yet.

I think if you want to take advantage of the non-persistent features of domain classes (e.g. validation, dependency injection, etc.) then it's fine to have some classes that could be database-backed but aren't. You just need to document that in the code or have some sort of convention, e.g. a special package structure or naming convention. If you never call GORM methods like save(), list(), findAllByFoo(), etc. then there won't be any database access.

As far as DTOs go, they can be un-DRY but there's a plugin that helps - see http://grails.org/plugin/dto. It hasn't been updated in a while but I'm pretty sure it still works. It has a nice feature where it will create a DTO instance from a persistent domain class instance with the syntax domainObj as DTO. You do need to keep changes synced between the classes, but the initial DTO generation is automatic via a script.

这篇关于Grails:当我不想保存任何内容时,我可以使用域对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆