简体   繁体   English

在Grails中使用特征进行水平域类重用,是个好主意吗?

[英]Using traits for horizontal domain class reuse in Grails, a good idea?

So I want to create 3 plugins which include domain classes and a restful service, and who each build on top of each other. 因此,我想创建3个包含域类和静态服务的插件,并且每个插件都相互构建。

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 (ie traits), I may very well just define everything in the trait and then implement the trait in the domain class. 然后我想到,由于Groovy具有水平重用功能(即特征),因此我很可能只在特征中定义所有内容,然后在域类中实现该特征。

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. 由于重命名字段,并且我失去了IDE自动完成的便利性,因此对我而言,组合域类不是一种选择。

My two questions are: 我的两个问题是:

  • In what part of the Grails project structure would it be best to place these traits. 最好将这些特征放在Grails项目结构的哪一部分。
  • Can this cause different problems? 这会引起其他问题吗?

The Trait source code should be in Trait源代码应位于

  1. Grails 2: src/groovy/[package][whatever.groovy] Grails 2: src / groovy / [package] [whatever.groovy]
  2. Grails 3: src/main/groovy/[package][whatever.groovy] Grails 3: src / main / groovy / [package] [whatever.groovy]

For example: src/main/groovy/com/my/package/foo.groovy 例如: 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. 并且everything将包含RecordPersonUser实例。 Kind of like a SQL union query. 有点像SQL联合查询。 When using Traits instead of inheritance you loose this ability. 使用特质而不是继承时,您会失去此能力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM