If I am understanding your use case correctly, it sounds like you may be 
interested in using the OptionalBinder that is new in guice4. It would 
allow you to set up a default binding (setDefault) in core, and then 
override that in a sub (setBinding).

The javadoc is fairly thorough and has some examples here:

http://google.github.io/guice/api-docs/latest/javadoc/com/google/inject/multibindings/OptionalBinder.html
 


On Tuesday, February 3, 2015 at 3:36:28 PM UTC-7, Aurélien wrote:
>
> Hi,
>
> I have a peculiar use case:
> - I have 3 libraries, Core, Sub and OtherSub
> - Sub and OtherSub do not know anything about each other, but they know 
> the existence of Core
> - Sub and OtherSub override the same functionality in Core
> - In a project, I want to combine Sub and OtherSub.
>
> It does not work, but here is how I wanted to implement this pattern with 
> Guice:
> - in the Core library:
>     o bind(A.class).to(ACore.class)
>     o class ACore implements A {
>         @Override public String a() { "a"; }
>     }
> - in the Sub library:
>     o bind(A.class).to(ASub.class)
>     o class ASub implements A {
>         private final A parent; // instance of ACore or AOtherSub
>         @Inject ASub(A parent) { this.a = parent; }
>         @Override public String a() { parent.a() + " overriden"; }
>     }
> - in the OtherSub library:
>     o bind(A.class).to(AOtherSub.class)
>     o class AOtherSub implements A {
>         private final A parent; // instance of ACore or ASub
>         @Inject ASub(A parent) { this.a = parent; }
>         @Override public String a() { parent.a() + " !"; }
>     }
>
> then in the projet using this set of libraries, I would have been able to 
> do:
> - Guice.createInjector(Modules.override(new ModuleCore()).with(new 
> AOtherSub())).getInstance(A.class).a() // returns "a !"
> - Guice.createInjector(Modules.override(Modules.override(new 
> ModuleCore()).with(new ASub())).with(new 
> AOtherSub())).getInstance(A.class).a() // returns "a overriden !"
> - Guice.createInjector(Modules.override(Modules.override(new 
> ModuleCore()).with(new AOtherSub())).with(new 
> ASub())).getInstance(A.class).a() // returns "a ! overriden"
>
> In this implementation, this throws a StackOverflowError because in ASub 
> or in AOtherSub the instance creation falls in an infinite loop.
>
> Is there a way of doing something like that with Guice ?
> Else do you have any insight on how I should implement this use case ?
>
> Cheers,
> Aurélien
>

-- 
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 http://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/940d9eaf-f9d4-44ad-9487-737b76a7b228%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to