[API Proposal]: Keyed services; alternative registration API for existing non-keyed services #109017
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-Extensions-DependencyInjection
untriaged
New issue has not been triaged by the area owner
Background and motivation
A lot of services exist that do not support keyed DI - they use
TryAddSingleton
etc internally, without any notion of keyed DI. Migrating those services to keyed-DI often requires the library-author to add additional keyed DI APIs for the purpose. This can also be a problem when we have a need for 2 instances of the same service to interact, such as decorator APIs - as discussed in this "extensions" topicThis is a proposal for an alternative API that works around this problem.
API Proposal
Consider an existing API of the form:
This adds SE.Redis as an
IDistributedCache
implementation on the default key. There is no current API to add SE.Redis as a keyed service, and the relevant type is not directly exposed, making it impossible to register as a keyed service manually.Now consider something along the lines of:
or
The key point here is that a decorator implementation of
IServiceCollection
is created which changes theAdd
method to add the specified key on non-keyed services. The 2 alternative suggestions have different ways of expressing the lifetime of the decorator; arguably the second version is clearer as to "here's the bits that are keyed", but I don't care to die on any hills.A rough proposed implementation of the idea is shown below and kind-of works. There is a complication, however: if we take the
AddStackExchangeRedisCache
as an example, this may use multiple other services, and if done naively, they'd all end up "keyed". I genuinely don't know whether keyed services work transitively, i.e. if a serviceFoo
is keyed "some key", and requires sub-serviceBar
without specifying a key, does it look for a keyed-service ("some key")Bar
and then fall back to the non-keyed serviceBar
, or does it only look for the non-keyedBar
? I wonder whether it might be necessary to say "I want to configure a specific type with a key", i.e.In this case, the decorator would only magically add the key for matching service types (presumably with an API to allow multiple types to be specified if required)
Rough implementation, for reference only, doesn't consider the "which service" complication:
API Usage
(shown above)
Alternative Designs
The alternative is to lean on library authors to add keyed-DI methods to all of their registration methods.
Risks
No response
The text was updated successfully, but these errors were encountered: