GWT-GIN Несколько реализаций? - PullRequest
3 голосов
/ 03 октября 2011

У меня есть следующий код

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();
}

В моей точке входа

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

Здесь ContactDetailView всегда связывается с ContactsDetailViewImpl.Но я хочу, чтобы при некоторых условиях это связывалось с ContactDetailViewImplX.

Как я могу это сделать?Пожалуйста, помогите мне.

1 Ответ

7 голосов
/ 03 октября 2011

Нельзя декларативно указывать Джину вводить одну реализацию иногда, а другую - в другое.Вы можете сделать это с помощью метода Provider или @Provides .

Provider Пример:

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 Пример:

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);
    }
}
...