2009/2/26 jonnybbb <[email protected]>
>
> Hi guys,
> i need to inject several implementions of the same interface into the
> same object.
>
> interface IService<I,O>{
> O execute(I);
> }
>
> class MyFacade {
> IService<InObj, OutObj> updateService;
> IService<InObj, OutObj> insertService;
> //Constructor setting the services
> // Methods executing the services
> }
>
> class InObj{}
> class OutObj{}
>
> Due to the same (Generics) signature of the service, i cannot profit
> from the TypeLiteral generics support of Guice.
> To do this in the Guice the User Guide recommends to use Annotations.
> As far as I understand this means i have to define several
> Annotations, one for each type of the Service.
> Or is there any other possible solution out there?
>
the only way to differentiate between bindings for the same type is by using
annotations,
but you can use Guice's @Named and Names.named() method to make this less
painful
@Inject
@Named("update")
IService<InObj, OutObj> updateService;
@Inject
@Named("insert")
IService<InObj, OutObj> insertService;
// you can also use @Named on injected constructor parameters
...then in your module...
TypeLiteral serviceType = new TypeLiteral<IService<InObj, OutObj>>(){};
bind(serviceType).annotatedWith(named("update")).to ...etc...;
bind(serviceType).annotatedWith(named("insert")).to ...etc...;
or something like that ;) HTH
Kind regards
> Johannes
> >
--
Cheers, Stuart
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---