On 27 Jul 2011, at 20:25, David Garcia wrote:

> I have some code like this:
> 
> Class A { }
> 
> 
> Class B {
>       @Inject A a;
> }
> 
> Class C {
> 
>       @Inject A a;
> }
> 
> Class D {
>       @Inject A a;
> 
>       @Inject B b;
> 
>       @Inject C c;
> }
> 
> bind(IfA).toProvider(Provider<A>)
> ...
> bind(IfC).toProvider(Provider<C>)
> 
> Is it possible to get the injections to work in such way that i get
> the following??
> 
> a1 = new A()
> b1 = new B(a1)
> c1 = new C(a1)
> d1 = new D(a1, b1, c1)
> 
> 
> a2 = new A()
> b2 = new B(a2)
> c2 = new C(a2)
> d2 = new D(a2, b2, c2)
> 
> So A and D instances must be 1 to 1, and A instance must be shared
> between B,C,D instances

sounds a lot like this question:

   
http://stackoverflow.com/questions/3014140/dependency-injection-scoping-by-region-guice-spring-whatever

general approach is to use a child injector to build instances of D, so A can 
be singleton scoped in that child injector

there are alternative solutions, such as creating your own custom scope 
(http://code.google.com/p/google-guice/wiki/CustomScopes) which can then hand 
out the same instance for a given context (per-thread, etc) but I think the 
child injector approach is simpler

> Or maybe any suggestions on how to refactor this in a better way??
> 
> Thanks
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "google-guice" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/google-guice?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to