Multiple A's indeed exist at the same time, but are typically created, together with their B's during a certain session scope. In situations where this is not the case we're using a factory now.
2016-10-21 10:27 GMT+02:00 Stephan Classen <[email protected]>: > Just I little question out of curiosity: > > Do multiple As exist at the same time or is the lifetime of an A strictly > separated (time wise) from the other As. > If multiple As exist at the same time. How do you decide to which A a B > belongs while it is requesting injection of C... > > > > On 21.10.2016 08:58, Tonni Tielens wrote: > > Thanks for the answer. In the mean time we've managed to get this working > by indeed using a custom scope. In our situation the lifetime of A is > indeed very well understood and A is only created at one point in the code > (the entry point of our application). So we expect that nobody would ever > need to enter or leave the scope manually in other points. > > 2016-10-20 17:47 GMT+02:00 Luke Sandberg <[email protected]>: > >> 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/ms >>> gid/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 a topic in the >> Google Groups "google-guice" group. >> To unsubscribe from this topic, visit https://groups.google.com/d/to >> pic/google-guice/yy6mVBxeZQE/unsubscribe. >> To unsubscribe from this group and all its topics, 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/ms >> gid/google-guice/CAO9V1MK0Ay9iztGy1NaEWZqb_PcCzUi9aUEaxNa1ey >> z_5ePCfw%40mail.gmail.com >> <https://groups.google.com/d/msgid/google-guice/CAO9V1MK0Ay9iztGy1NaEWZqb_PcCzUi9aUEaxNa1eyz_5ePCfw%40mail.gmail.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/CALSKP0zCNm7WuU0SBXJjHSR5Pi- > FCa0jKb-O88hViDNMq%3Dc%3DKw%40mail.gmail.com > <https://groups.google.com/d/msgid/google-guice/CALSKP0zCNm7WuU0SBXJjHSR5Pi-FCa0jKb-O88hViDNMq%3Dc%3DKw%40mail.gmail.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 a topic in the > Google Groups "google-guice" group. > To unsubscribe from this topic, visit https://groups.google.com/d/ > topic/google-guice/yy6mVBxeZQE/unsubscribe. > To unsubscribe from this group and all its topics, 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/f3284ce3-ec87-4a44-18d2-2f13ea48daf9%40gmx.ch > <https://groups.google.com/d/msgid/google-guice/f3284ce3-ec87-4a44-18d2-2f13ea48daf9%40gmx.ch?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/CALSKP0w0PgSUgQB5-ACC%2BU_8Fha0c%3DM-WHseQ09xSm2iKc%3DwUQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
