1

I need similar object as grails domain object which does not need persisting. 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. Although all related objects and collections will need to be redone.

  • 1
    Domain class contains a lot of other things also. So instead of extending the domain class, put your fields in a groovy file and extend that in your domain class. – Sandeep Poonia Jan 06 '16 at 06:58
  • 3
    also consider using a `trait` to not lock yourself into some inheritance hell – cfrick Jan 06 '16 at 07:45

1 Answers1

2

@cfrick is spot on. A Groovy trait is a very good way to go. 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.
         */  
    }   
}
Emmanuel Rosa
  • 9,697
  • 2
  • 14
  • 20