简体   繁体   English

扩展grails域对象是一个好主意吗?

[英]Is it a good idea to extend a grails domain object?

I need similar object as grails domain object which does not need persisting. 我需要与grails域对象类似的对象,不需要持久化。 In order that I do not have to make changes in 2 places for any field changes, is it a good idea to extend the domain class so that I can get the benefits of single set of fields. 为了不必对任何字段更改都在2个地方进行更改,扩展域类是一个好主意,以便我可以获得单个字段集的好处。 Although all related objects and collections will need to be redone. 尽管所有相关对象和集合都需要重做。

@cfrick is spot on. @cfrick现场。 A Groovy trait is a very good way to go. Groovy特性是一个很好的选择。 You can get a full example here (bad name for the project, I know). 您可以在此处获得完整的示例(我知道该项目的名字不好)。 Here's a quick example: 这是一个简单的示例:

// MyTrain.groovy: Put this in src/main/groovy/my/package
package my.package

trait MyTrait {
    Integer number
    String something
}

// MyDomainClass.groovy: This goes with the other domain classes.
package my.package

class MyDomainClass implements MyTrait {
    /*  
     * number and something properties are available here.
     * They become table columns.
     */  

    static constraints {
        /*  
         * And you can place constraints on them,
         * as it they had been declared in this class.
         */  
    }   
}

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

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