Looks like you want something like this:

bind(FooComponent.class).to(FooComponentImpl.class);
bind(FooCompmentImpl.class).in(Scopes.SINGLETON);

See:
http://code.google.com/docreader/#p=google-guice&s=google-guice&t=Scopes.
 Specifically:

======

In linked bindings, scopes apply to the binding source, not the binding
target. Suppose we have a class Applebees that implements both Bar and
Grill interfaces.
These bindings allow for *two* instances of that type, one for Bars and
another for Grills:

  bind(Bar.class).to(Applebees.class).in(Singleton.class);
  bind(Grill.class).to(Applebees.class).in(Singleton.class);

This is because the scopes apply to the bound type (Bar, Grill), not the
type that satisfies that binding (Applebees). To allow only a single
instance to be created, use a @Singletonannotation on the declaration for
that class. Or add another binding:

  bind(Applebees.class).in(Singleton.class);

=======

Cheers,
Chris

On Fri, Mar 26, 2010 at 5:32 PM, Russ <[email protected]> wrote:

> 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]<google-guice%[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