Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom Guice Scope, or a better approach?
    text
    copied!<p>Here's my problem:</p> <p>It's first important to know that I'm writing a simulation. This is a standalone application, and is single-threaded. I have essentially two classes of objects that have different scoping requirements.</p> <ol> <li><p>Classes that should be used as singletons throughout the entire simulation. An instance of Random, as an example.</p></li> <li><p>Groups of classes that are created together, and within the group, each instance should be treated like a Singleton. For example, say <code>RootObject</code> is the top level class, and has a dependency to <code>ClassA</code> and <code>ClassB</code>, both of which have a dependency to <code>ClassD</code>. For any given <code>RootObject</code>, both of its dependencies (<code>ClassA</code> and <code>ClassB</code>) should depend on the same instance of <code>ClassD</code>. However, instances of <code>ClassD</code> should not be shared across different instances of <code>RootObject</code>.</p></li> </ol> <p>Hopefully that makes sense. I can think of two approaches to this. One is to mark all of the injected objects as Singletons, create the root injector, and spin off a child injector each time I need to create a new <code>RootObject</code> instance. Then, the instances of <code>RootObject</code> and all of its dependencies are created as Singletons, but that scoping information is thrown away the next time I go to create another <code>RootObject</code>.</p> <p>The second approach is to implement some type of custom scope.</p> <p>The Guice documentation gives conflicting advice... On one hand, it says that you should have a single injector, and that ideally it is called once to create some top level class. On the other hand, it says to stay away from custom scopes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload