Another approach is to wrap your application modules inside a rewriting module that uses the Guice SPI to scan their binding elements for dependencies, for example: new WireModule(moduleA, moduleB, moduleC). Any missing dependencies can be bound at the end of the analysis with placeholder providers - for example that throw an exception on get(), or redirect the dependency lookup via another mechanism (services, etc). You can then either replace these placeholder dependencies (again by searching the bindings with the SPI) when you create a child injector, or use the aforementioned service mechanism to perform the late lookup. Note all of this can be done before you create any injector - that's a key benefit of the declarative Module API (and SPI).
This is what we use over in http://eclipse.org/sisu/ to add dynamics to Guice applications: http://git.eclipse.org/c/sisu/org.eclipse.sisu.inject.git/tree/org.eclipse.sisu.inject/src/org/eclipse/sisu/binders/WireModule.java http://git.eclipse.org/c/sisu/org.eclipse.sisu.inject.git/tree/org.eclipse.sisu.inject/src/org/eclipse/sisu/binders/LocatorWiring.java PS. as part of the move to Eclipse I'm cleaning up the API/SPI and writing some docs so people can re-use this helper code, so questions/comments are very welcome (http://eclipse.org/sisu/support/) On 29 Jul 2012, at 22:18, Fedor Li wrote: > Hi Fred! Thanks for your reply! > > Idea number 2 has to be ruled out, because it doesn't fit to my usecase. > Besides, AssistedInject is rather rigid, for example when it comes to > circular dependencies - then AssistedInject simply doesn't work (at least in > my tests) ... > > The first idea would be doable, but exactly the point that you mentioned at > the end about the costs of creating injectors brought me to my idea with > child injectors. I thought that I could decrease performance penalty with > that approach. > > On Sunday, July 29, 2012 12:08:08 AM UTC+2, Fred Faber wrote: > I suspect there's another solution to the problem you're trying to solve > here. Without knowing what that is, here are my guesses: > > 1) you don't actually need the parent injector, and you can pass an > incomplete Module around until you need to create an injector, at which point > you can combine that Module with the Module with your additional bindings in > it > 2) you use the parent injector, but want to use a runtime parameter to create > objects of the same type in different contexts. In that case, AssistedInject > should do what you want. > > Keep in mind also that creating an injector can be expensive. > > Fred -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
