There are two common options 1. you could use assisted injection or @AutoFactory to create a factory for the b's that allow you to pass an explicit object. Then A can just construct a C and pass it to all of its Bs 2. you could introduce a custom scope managed by a1 and a2. This is sometimes appropriate but can be a fair bit of work to implement and manage
I generally recommend against custom scopes unless 1. the lifetime is very well understood and generally strictly contained within another scope (for example, if you are using RequestScope, your custom scopes probably shouldn't span requests without a lot of care to prevent request data from 'leaking') 2. there are a small number (ideally 1) of ways to 'enter' the scope. 3. you have exhausted other options :) like assisted injection. Also, in general, Guice doesn't need to control every part of your application, so if the relationships between A B and C don't map well to a guice concept you could always just handle that part of your application 'manually' and let guice handle the rest. On Thu, Oct 13, 2016 at 12:00 AM Tonni Tielens <[email protected]> wrote: > Hi all, > > We're working on quite a big project, already using dependency injection > in 90% of the places, but without a framework. After having to modify 33 > files just to add one dependency into one deeply nested class, I'm > convinced that we should use a DI framework. Google Guice is one of the > candidates. I've read the guide and learned about scopes, but one thing > that is unclear to me is the following. > > What we have is something like this. Class A, that contains one or more > objects of type B, that each contain a reference to the same object of type > C. My question is about C. You might think that C should be a singleton, > but actually it's a bit more complex. The scope of C depends on A. Hence, > for each A there should be one C. And all the B's in that A instance should > have a reference to that instance of C, while all the B's in the other A's > instance, should have a reference to the other C instance. > > So something like this: > > a1 -> b1 -> c1 > a1 -> b2 -> c1 > a1 -> b3 -> c1 > > a2 -> b4 -> c2 > a2 -> b5 -> c2 > a2 -> b6 -> c2 > > Is this possible using Guice? How would one do this? > > Regards, > > Tonni Tielens > > -- > You received this message because you are subscribed to the Google Groups > "google-guice" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/google-guice. > To view this discussion on the web visit > https://groups.google.com/d/msgid/google-guice/c2184ab4-6a4f-40e2-92f5-c1ed36e34863%40googlegroups.com > <https://groups.google.com/d/msgid/google-guice/c2184ab4-6a4f-40e2-92f5-c1ed36e34863%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/google-guice. To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/CAO9V1MK0Ay9iztGy1NaEWZqb_PcCzUi9aUEaxNa1eyz_5ePCfw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
