3

I have the following code

public class AppGinModule extends AbstractGinModule{
    @Override
    protected void configure() {
        bind(ContactListView.class).to(ContactListViewImpl.class);
        bind(ContactDetailView.class).to(ContactDetailViewImpl.class);
    }
}

@GinModules(AppGinModule.class) 
public interface AppInjector extends Ginjector{
    ContactDetailView getContactDetailView();
    ContactListView getContactListView();
}

In my entry point

AppInjector appInjector = GWT.create(AppGinModule.class);
appInjector.getContactDetailsView();

Here ContactDetailView is always bind with ContactsDetailViewImpl. But i want that to bind with ContactDetailViewImplX under some conditions.

How can i do that? Pls help me.

Kanagaraj M
  • 639
  • 5
  • 16
  • What are the conditions under which you want to substitute different implementations? For different browsers? – Daniel Oct 03 '11 at 04:59
  • 1
    No. It's for user privileges. Two views for two different set of users. As like when we use factories we will get different implementations based on some condition. – Kanagaraj M Oct 03 '11 at 05:02

1 Answers1

7

You can't declaratively tell Gin to inject one implementation sometimes and another at other times. You can do it with a Provider or a @Provides method though.

Provider Example:

public class MyProvider implements Provider<MyThing> {
    private final UserInfo userInfo;
    private final ThingFactory thingFactory;

    @Inject
    public MyProvider(UserInfo userInfo, ThingFactory thingFactory) {
        this.userInfo = userInfo;
        this.thingFactory = thingFactory;
    }

    public MyThing get() {
        //Return a different implementation for different users
        return thingFactory.getThingFor(userInfo);
    }   
}

public class MyModule extends AbstractGinModule {
  @Override
  protected void configure() {
      //other bindings here...

      bind(MyThing.class).toProvider(MyProvider.class);
  }
}

@Provides Example:

public class MyModule extends AbstractGinModule {
    @Override
    protected void configure() {
        //other bindings here...
    }

    @Provides
    MyThing getMyThing(UserInfo userInfo, ThingFactory thingFactory) {
        //Return a different implementation for different users
        return thingFactory.getThingFor(userInfo);
    }
}
Daniel
  • 10,115
  • 3
  • 44
  • 62