2011-10-03 3 views
3

내 진입 점에서 다음 코드를GWT-GIN 다중 구현?

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

여기 ContactDetailView 항상 ContactsDetailViewImpl과 결합되어

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

있습니다. 그러나 나는 그것이 어떤 조건 하에서 ContactDetailViewImplX과 묶이기를 원한다.

어떻게 할 수 있습니까? Pls 날 도와 줘요.

+0

? 다른 브라우저의 경우? – Daniel

+1

아니요. 사용자 권한입니다. 서로 다른 두 사용자 집합에 대한 두 개의보기 공장을 사용할 때와 마찬가지로 우리는 어떤 조건에 따라 다른 구현을 할 것이다. –

답변

7

Gin에 선언 구현시 특정 구현을 삽입하고 다른 구현에는 다른 구현을 삽입하도록 선언 할 수 없습니다. 그래도 Provider 또는 @Provides method으로 처리 할 수 ​​있습니다.

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); 
    } 
} 
+0

고맙습니다. 이게 내가 찾는거야. –

관련 문제