1
votes

I have been using google-guice for few months now. I'm quite satisfied with it, but it seems that i'm using it wrong. I'm creating a lot of assisted-injection and sometimes two injectors.

Hence i would like to understand the general principle here.

Does one should have one injector for the all application that wires everything from the main class ?

Would the best practice be about, trying to inject has much in the constructor from the start of the application ? (i see the advantage in testing)

My main problem with this is that sometimes i feel like the creation of some groups of objects belong to certain component. That there is a need to encapsulate that? How would you deal with that?

When you have an important deep structure of interconnected lservices, how do you do to pass the injector or the object required. You must find a way ? I have resorted a lot in assisted injection.

Even when the parameter could have been created outside of the object, i still prefer to let the service create the parameter and pass it to the assisted factory. Because the creation of the parameter sometimes involve some logic that belongs to the service (sometimes)

Any suggestion would help

Many thanks

1
I like the question, but it might help to discuss some concrete examples of yours ... - Jan Galinski

1 Answers

1
votes

Does one should have one injector for the all application that wires everything from the main class?

Usually, yes. One injector for the application ; and one injector for the tests.

This of course depends how the modules depends on each other, and whether it's a command-line application or a web application.

Would the best practice be about, trying to inject has much in the constructor from the start of the application? (i see the advantage in testing)

Yes.

  • Either you load all your modules directly
  • Or, if you want more control on the set of modules, you can create the injector with a single module that installs other modules.

how do you do to pass the injector or the object required.

With @Assisted or FactoryModuleBuilder. See assisted injection.