最佳实践 - 通过静态字段获取 grails 中的域对象?

发布于 2024-10-07 07:35:10 字数 261 浏览 3 评论 0原文

因此,我们的一些 grails 对象(参考数据)有独特的“代码”,当我们想要检索它们时,我们通过使用它们的静态代码调用它们来实现:

Currency.findByCode(Currency.DOLLAR)

也许我完全错了,但这似乎是一个非常好的方法。检索对象的冗长且非常规的方式(特别是当您必须为多个对象执行多次时)。

是否有更可接受的方法(也许在某处引用了对象本身)?如果这是最好的方法,那么这是一个可以接受的答案。谢谢。

So we have unique 'codes' for some of our grails objects (ref data), and when we want to retrieve them, we do so by calling them with their static code:

Currency.findByCode(Currency.DOLLAR)

Perhaps I'm completely wrong, but this seems like a very verbose and non-groovy way of retrieving objects (especially when you have to do it multiple times, for multiple objects).

Is there a more accepted approach (maybe having a reference to the object itself somewhere)? If this is the best way to do it, that is an acceptable answer. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放低过去 2024-10-14 07:35:10

使用静态变量,您可以做的另一件事是使用静态导入来缩短代码(这实际上是 Java 的一部分,但直到我转向 groovy 才找到它):

如果您 类顶部的CurrencyType(可能是保存您定义的各种类型的货币的枚举):

static import com.example.CurrencyType.*

在代码中,您不再需要为所有引用添加前缀 CurrencyType,您可以这样做:

Currency.findByCode(DOLLAR)

如果不需要更改,您还可以向您的货币类添加一个辅助方法来检索它:

Currency.groovy:
static import com.example.CurrencyType.*
...
static transients = ['dollar']
...
static Currency getDollar() {
    Currency.findByCode(DOLLAR)
}

这将允许您在其他代码中使用 Currency.dollar 。在这些类中,您还可以使用静态导入来引用 dollar

static import com.example.Currency.*
....
println dollar // gets the dollar from the db and prints it out

One other thing that you could do to shorten up the code if you use static variables is to use static imports (this is actually part of Java, but I didn't find it till I moved to groovy):

If you do a static import of CurrencyType (potentially an enum holding the various types of Currency that you've defined) at the top of your class:

static import com.example.CurrencyType.*

Down in your code, you no longer need to prefix all of the references with CurrencyType, you can just do:

Currency.findByCode(DOLLAR)

If they don't need to change, you could also add a helper method to your Currency class to retrieve it:

Currency.groovy:
static import com.example.CurrencyType.*
...
static transients = ['dollar']
...
static Currency getDollar() {
    Currency.findByCode(DOLLAR)
}

That would let you use Currency.dollar in your other code. In those classes, you could also use a static import there to just refer to dollar:

static import com.example.Currency.*
....
println dollar // gets the dollar from the db and prints it out
迷乱花海 2024-10-14 07:35:10

这取决于。这看起来像是参考数据。如果参考数据永远不会改变,我根本不会使用持久层——我会编写一堆静态变量作为静态参考数据。

如果您希望能够更改参考数据而不重新部署,最实用的方法是从数据库加载它们。您将拥有某种类型的管理屏幕,您可以在其中操作数据。您可以使用像 ehcache 这样的二级缓存来限制持久层实际访问数据库的数量——这样您可以获得非常好的性能。请参阅用户指南第 5.5.2.2 节。

但是,使用当前的方法,您必须根据参考数据的更改进行重新部署,因为需要对Currency.DOLLAR 进行编码。最好不必这样做。

It depends. This seems like reference data. If the reference data is never gonna change, I wouldn't use the persistence layer at all -- I would code up a bunch of static variables that are the static reference data.

If you want to be able to change your reference data without redeploying, the most practical way would be to load them from the db. You would have some type of admin screen where you could manipulate the data. You would use 2nd level cache like ehcache to limit how much the persistence layer actually hit the db -- you can get really good performance this way. See section 5.5.2.2 of the user guide.

However, with your current approach you would have to redeploy on a change in your reference data, because the Currency.DOLLAR need to be coded in. It would probably be nice to not have to do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文