Hey,

As you know we do not have names for Services, and especially on the 
@Service injection there is no way to specify "id" or "name" of the 
service to be injected if there are several ones that meet the requirements.

There are mainly two ways to get around this. Either one can inject 
Iterable<ServiceType> and select one from the list in the actual code, 
or one can add a Service of the specified type to the Module in the 
Assembler and have it do the filtering. Here's an example of how it 
could work:
module.addObjects( ServiceConsumer.class );

module.addServices( TestService.class ).
     providedBy( ServiceIdFilter.class ).
     setServiceAttribute( String.class, 
TestServiceComposite2.class.getSimpleName() );

module.addServices( TestServiceComposite1.class,
                     TestServiceComposite2.class);
---

So, even if there are two TestServiceComposite's registered, which both 
implement TestService, I add another one before them which does a lookup 
on the given type (TestService) and finds the one with the given id. 
This can be expanded to more advanced checks, to test for other 
attributes or similar. In this case there is no need for the 
ServiceConsumer to do any selection itself, it just does @Service 
TestService and it will have the "correct" one injected.

This approach does not handle the case that different injection points 
in the same module wants different services injected, i.e. they will all 
get the same.

Is this good enough? Can you see any scenarios where you would want/need 
even more control? And if so, any suggestions on how that should work?

Note that the above pattern can also be used for Service proxy/policy 
purposes, e.g. instead of id filtering the custom provider could select 
the first Service in the list to begin with and on exception or 
unavailability move to the next Service available.

/Rickard

_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev

Reply via email to