【问题标题】:Error when using interface in grails domain class在 grails 域类中使用接口时出错
【发布时间】:2014-05-29 10:16:38
【问题描述】:

我正在尝试使用 grails 实现接口基域类,但在运行时遇到错误。错误类型为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
    ..............
}

我认为因为我没有在域类 C 中指定“MyInterface”的真正实现,所以 grails 在启动时注入 bean 时会遇到麻烦。但我想让MyInterface 尽可能抽象,因为不同的类会以各种方式实现它。有没有办法克服这个错误?我可以在没有域类扩展的情况下构建模型吗(我想尽可能避免这种情况)?

【问题讨论】:

    标签: grails groovy


    【解决方案1】:

    仅当您想要持久化数据时才需要域类。在不了解具体情况的情况下,您可能希望创建类 A、B 和 C 服务而不是域类。根据我的经验,子类和接口在服务领域比领域类领域更有效。我想每个 returnSomething() 实现都将在不需要接口的不同域类上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多