Hi guys,

I have a situation where a nested hierarchy of private modules
contains
bindings to the same type. The bindings work because the private
modules
isolate these bindings from one another and are only exposed to their
parent modules. An example of this is as follows:


    public static interface D {
    }

    public static class DImpl1 implements D {
    }

    public static class DImpl2 implements D {
    }

    public static interface C {

        public D getD();
    }

    public static class CImpl implements C {

        private final D d;

        @Inject
        public CImpl(D d) {
            this.d = d;
        }

        @Override
        public D getD() {
            return this.d;
        }
    }

    public static interface B {

        public D getD();
    }

    public static class BImpl implements B {

        private final D d;

        @Inject
        public BImpl(D d) {
            this.d = d;
        }

        @Override
        public D getD() {
            return this.d;
        }
    }

    public static interface A {

        public B getB();

        public C getC();
    }

    public static class AImpl implements A {

        private final B b;
        private final C c;

        @Inject
        public AImpl(B b, C c) {
            this.b = b;
            this.c = c;
        }

        @Override
        public B getB() {
            return this.b;
        }

        @Override
        public C getC() {
            return this.c;
        }
    }

    @Test
    public void test() {

        Module defaults = new AbstractModule() {

            @Override
            protected void configure() {
                bind(A.class).to(AImpl.class);
                bind(B.class).to(BImpl.class);
                bind(C.class).to(CImpl.class);
                bind(D.class).to(DImpl1.class);
            }
        };

        Module overrides = new AbstractModule() {

            @Override
            protected void configure() {

                install(new PrivateModule() {

                    @Override
                    protected void configure() {
                        install(new PrivateModule() {

                            @Override
                            protected void configure() {
                                bind(D.class).to(DImpl1.class);
                                expose(D.class);
                            }
                        });
                        bind(B.class).to(BImpl.class);
                        expose(B.class);
                    }
                });

                install(new PrivateModule() {

                    @Override
                    protected void configure() {
                        install(new PrivateModule() {

                            @Override
                            protected void configure() {
                                bind(D.class).to(DImpl2.class);
                                expose(D.class);
                            }
                        });
                        bind(C.class).to(CImpl.class);
                        expose(C.class);
                    }
                });

                bind(A.class).to(AImpl.class);
            }
        };

        Injector injector = Guice.createInjector(overrides);
        A aInstance = injector.getInstance(A.class);

        Assert.assertThat(aInstance.getB().getD(),is(instanceOf
(DImpl1.class)));
        Assert.assertThat(aInstance.getC().getD(),is(instanceOf
(DImpl2.class)));
    }

The bindings to DImpl1 and DImpl2 work because of the isolation that
the
private modules provide. However, if I replace the line:

Injector injector = Guice.createInjector(overrides);

with:

Injector injector = Guice.createInjector(Modules.override
(defaults).with(overrides));


Guice complains that there is already a binding to D. From what I saw
in
Guice's unit tests is that I need to expose D's bindings upwards (ie.
I
need to add expose(D.class) calls in all of the private modules) in
the
hierarchy to get the overrides to work. However, the problem with this
is that I will get a binding conflict (between the different bindings
of
D.class) if I do this.

Is there any way I can override the bindings in the default module
with
the nested hierarchy of bindings, without exposing the bindings in the
nested hierarchy of private modules?

Regards,

Wiehann

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" 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-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to