Consider the following:

I create the following interface:

interface FooComponent {
}

that I make my own extension of a third party library's class
implement:

class FooComponentImpl extends ThirdPartyFooComponent
    implements FooComponent {
}

I add "shadow" methods to FooComponent to allow me to access
FooComponentImpl (and, more importantly, ThirdPartyFooComponent)
methods via the FooComponent interface.

I want to keep ModelImpl agnostic to this third party library, so I
write it in terms of FooComponent interface instead of
FooComponentImpl:

class ModelImpl implements Model {

    @Inject private FooComponent _comp;

}

This allows me to inject a fake FooComponent for a unit test:

class FakeFooComponent implements FooComponent {
}

ViewImpl however needs to be written in terms of the Third Party
library's classes in order to use that library's API:

class ViewImpl extends ThirdPartyContainer
    implements View {

    @Inject private FooComponentImpl _comp;

    void init() {
        // add() is a ThirdPartyFooComponent API method written
        // in terms of ThirdPartyFooComponent
        add(_comp);
    }
}

I never plan on injecting anything other than a FooComponentImpl into
ViewImpl.

How do I inject the same instance into both ModelImpl and ViewImpl,
given that they are different types?  I can annotate each with
@Named("component1") and add that to the binding:

bind(FooComponent.class)
    .annotatedWith(Names.named("component1")
    .to(FooComponentImpl.class)
    .in(Scopes.SINGLETON);

but Guice can't resolve the inject request in ViewImpl because it's
written in terms of FooComponentImpl, not FooComponent.

Is what I'm asking for even possible to do in Guice?  I really don't
want to have to write ViewImpl like this:

class ViewImpl extends ThirdPartyContainer
    implements View {

    @Inject private FooComponent _comp;

    void init() {
        // add() is a ThirdPartyFooComponent API method written
        // in terms of ThirdPartyFooComponent
        add(ThirdPartyFooComponent.class.cast(_comp));
    }
}

Thanks in advance for your time.

-Russ

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