0

Ok, so the situation is not as simple as in the title. Here's the deal.

I have a class ComplexClass that expects several arguments to be injected into it. Argument "A" is defined in module A, and argument "B" is defined in module B. Now, the argument "C", on the other hand, is defined in module C1 and module C2.

I have a single injector that is created with all four of the above modules. Sorry, but it must be a single injector.

Now, I would like to create two different instances of ComplexClass -- one with the third argument being from module C1 and the second one from module C2.

Thanks!

  • Ok, so I figured a way - simply let each C module define a @Provides method where they annotate the third constructor argument with theirs. However, is there a way to do it so the modules don't have to even know that there exist other arguments? So just specify that any C-argument must be theirs? – user1497437 Jul 03 '12 at 01:34
  • Annotations (no @Provides) should be sufficient, no? – Sohail Jul 03 '12 at 14:26

1 Answers1

0

This problem is generally referred to as the "Robot Legs Problem".

See: http://code.google.com/p/google-guice/wiki/FrequentlyAskedQuestions#How_do_I_build_two_similar_but_slightly_different_trees_of_objec

And see Jesse Wilson's answer here: How do I bind Different Interfaces using Google Guice?

According to the PrivateModules solution, you'd just install the modules for C1 and C2 inside of PrivateModules which each bind your ComplexClass with a different binding annotation.

There are other possible solutions, too, like using assisted injection to inject the dependency that may come from one of two places.

Community
  • 1
  • 1
Andrew McNamee
  • 1,705
  • 1
  • 13
  • 11