简体   繁体   English

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

[英]Best practice - Getting a domain object in grails by a static field?

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: 因此,我们为某些grails对象(引用数据)提供了唯一的“代码”,当我们想要检索它们时,可以通过使用其静态代码进行调用来实现:

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. 谢谢。

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): 如果使用静态变量,可以缩短代码的另一件事是使用静态导入(这实际上是Java的一部分,但是直到我转向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: 如果您在类的顶部进行了CurrencyType的静态导入(可能是一个枚举,其中包含您定义的各种类型的Currency):

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: 在代码中,您不再需要为所有引用添加CurrencyType前缀,您可以执行以下操作:

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类中添加一个辅助方法来检索它:

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. 那将使您可以在其他代码中使用Currency.dollar In those classes, you could also use a static import there to just refer to dollar : 在这些类中,您还可以在其中使用静态导入来仅引用dollar

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

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. 您将使用ehcache之类的第二级缓存来限制持久层实际到达数据库的数量-这样您就可以获得非常好的性能。 See section 5.5.2.2 of the user guide. 请参阅用户指南的5.5.2.2节。

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. 但是,使用当前的方法,您将不得不重新部署参考数据中的更改,因为需要输入Currency.DOLLAR。不必这样做可能会很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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