As Sam said, a MapBinder seems more appropriate here.  Otherwise you have
to build a Key and go into the Injector with that Key to get the Binding,
which gets ugly pretty quick.


On Tue, Sep 24, 2013 at 8:50 AM, Sam Berlin <[email protected]> wrote:

> You may want to consider using 
> MapBinder<https://code.google.com/p/google-guice/wiki/Multibindings>,
> and bind a map of <String, Communication>.  Then users inject the Map
> (optionally as a Map<String, Provider<Communication>>) and call
> .get(aString).
>
>  sam
>
>
> On Mon, Sep 23, 2013 at 5:05 PM, Rui Pacheco <[email protected]> wrote:
>
>> (Also posted on stackoverflow<http://stackoverflow.com/q/18968410/2808692>
>> )
>>
>> I have several classes implementing interface Provider<Communication> and
>> I'm using Guice with a @Named annotation to bind them as required, e.g.:
>>
>> @Singletonpublic class Verizon implements Provider<Call> {
>>   ...}
>> @Singletonpublic class TMobile implements Provider<Call> {
>>   ...}
>>
>> bind (new TypeLiteral<Provider<Call>>() {}).annotatedWith(
>>   Names.named("Verizon")).to(Verizon.class);
>>
>> bind (new TypeLiteral<Provider<Call>>() {}).annotatedWith(
>>   Names.named("TMobile")).to(TMobile.class);
>>
>> Is there a clean way to implement a factory that takes the name as a
>> parameter, e.g.:
>>
>> public static <C extends Communication> Provider<C> getCallProvider(C 
>> communication) {
>>   String providerName = communication.getProviderName();
>>
>>   return [Guice's matching object for type Provider<?> and @Named = 
>> providerName];}
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "google-guice" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/google-guice.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/google-guice.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to