Making scope depending on lifetime of another object

71 views
Skip to first unread message

Tonni Tielens

unread,
Oct 13, 2016, 3:00:11 AM10/13/16
to google-guice
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

Luke Sandberg

unread,
Oct 20, 2016, 11:47:35 AM10/20/16
to google-guice
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.

--
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 google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
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.
For more options, visit https://groups.google.com/d/optout.

Tonni Tielens

unread,
Oct 21, 2016, 2:58:32 AM10/21/16
to google...@googlegroups.com
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.

To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+unsubscribe@googlegroups.com.

--
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 google-guice+unsubscribe@googlegroups.com.

To post to this group, send email to google...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.

Stephan Classen

unread,
Oct 21, 2016, 4:27:23 AM10/21/16
to google...@googlegroups.com

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...

To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.

To post to this group, send email to google...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.

Tonni Tielens

unread,
Nov 1, 2016, 6:11:23 AM11/1/16
to google...@googlegroups.com
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.

--
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 google-guice+unsubscribe@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
Reply all
Reply to author
Forward
0 new messages