简体   繁体   中英

Error when using interface in grails domain class

I am trying to impelement an interface base domain class with grails and I am runing into error at run time. The error type is Could not determine type for: com.testApp.MyInterface , at table: C .

// in /src/groovy directory
interface    MyInterface{
    int    returnSomething()
}

// in domain class directory 
class   A   implements MyInterface{
    int returnSomething(){
    //a first implementation here
  }
}

class B implements MyInterface{
    int returnSomething(){
    //a second implementation here
  }
}

class C {
    ....
    MyInterface type
    ..............
}

I think because I'm not specifying the real implementation of "MyInterface " in domain class C, grails is getting into trouble when injecting beans at startup. But I would like to keep MyInterface as abstract as possible because differents classes will implement it, various ways. Is there a way to overcome this error? Can I build my models without domain classes extension (I want to avoid that as far as it is possible)?

You need domain classes only if you want to persist data. Without knowing the specifics of your situation, you may want to make classes A, B, and C services rather than domain classes. It has been my experience that sub-classes and interfaces work better in the service realm than the domain class realm. I would imagine that each returnSomething() implementation would operate over distinct domain classes that do not require interfaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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