在Grails中使用特征来实现水平域类的重用,这是一个好主意吗? [英] Using traits for horizontal domain class reuse in Grails, a good idea?
问题描述
从概念上讲,它们将继承这样的基本模型:
Record> Person> User
然而,我已阅读友好手册继承可能会导致一些性能问题。然后它让我想到了,由于Groovy具有横向重用功能(即特性),我可能只需定义特性中的所有内容然后在域类中实现该特征。
由于字段的重命名,编写域类不是我的选择,而且方便性的丧失的IDE自动完成。
我的两个问题是:
- 部分Grails项目结构最好放置这些特征。
- 这会导致不同的问题吗?
- Grails 2: src / groovy / [package] [whatever。
- Grails 3: src / main / groovy / [package] [whatever.groovy]
- In what part of the Grails project structure would it be best to place these traits.
- Can this cause different problems?
- Grails 2: src/groovy/[package][whatever.groovy]
- Grails 3: src/main/groovy/[package][whatever.groovy]
例如: src / main / groovy / com / my / package / foo.groovy
您将遇到的主要问题是您将失去执行多态查询的能力。例如,对于继承,您可以这样做:
def everything = Record.list()
code>
和 everything
会包含记录
, Person
和 User
实例。有点像SQL联合查询。当使用特质而不是继承时,你放弃了这种能力。
So I want to create 3 plugins which include domain classes and a restful service, and who each build on top of each other.
Conceptually, they would "inherit" the base model this way:
Record > Person > User
However I have Read From The Friendly Manual that inheritance may cause some performance issues.
Then it crossed my mind that since Groovy has horizontal reuse capabilities (i.e. traits), I may very well just define everything in the trait and then implement the trait in the domain class.
Composing domain classes is not an option for me because of the renaming of the fields, and well, the loss of the convenience of IDE auto-completion.
My two questions are:
The Trait source code should be in
For example: src/main/groovy/com/my/package/foo.groovy
The main issue you'll have is that you'll loose the ability to perform polymorphic queries. For example, with inheritance you can do something like this:
def everything = Record.list()
and everything
would contain Record
, Person
, and User
instances. Kind of like a SQL union query. When using Traits instead of inheritance you loose this ability.
这篇关于在Grails中使用特征来实现水平域类的重用,这是一个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!