如何使用 GORM 以 DRYish 的方式抽象出数据源选择

How to abstract out datasource selection in a DRYish way using GORM

我有一个域设置了 n 个数据源(我们在这里称之为 2)。我目前必须以这种方式访问​​查找对象等...

// DS1
Item.find(id)
// DS2
Item.ds2.find(id);

这在一个小的逻辑函数上工作正常,但是当有很多查找和保存时,它会产生一个非常不干燥的环境...

if(isDs1){
  Item.find(id)
  ...
}
else{
  Item.ds.find(id)
  ...
}

我在 JS 中想这样的事情...

String ds = isDs1 ? 'ds1' : 'ds2'
Item[ds].find(id)

但这在 Groovy(?)

中是不可能的

还有另一种方法可以以相当干的方式做到这一点吗?

更新

对于那些感到困惑的人,我的 DataSource.groovy 看起来像这样...

environments {
  development {
    datasource_ds1{
      ...
    }
    datasource_ds2{
      ...
    }
  }
}

Groovy 支持动态调用。相当于您的 javascript 示例是这样的:

String ds = isDs1 ? 'ds1' : 'ds2'
Item."${ds}".find(id)