Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Named" Services Registrations #19

Open
carusology opened this issue Jan 24, 2018 · 0 comments
Open

Add "Named" Services Registrations #19

carusology opened this issue Jan 24, 2018 · 0 comments

Comments

@carusology
Copy link
Contributor

One of the major qualms I have had with the Microsoft dependency injection implementation is that it has a one-type / one-registration policy. If I have two implementations of IFoo, I cannot register them both simultaneously. Instead, I need to recreate them every time I need them.

StructureMap, because it is not terrible, uses the concept of "named" registrations to get around this problem. When you register an implementation of a service to an interface, you can "name" that registration with a string.

We could add this functionality to the Microsoft implementation by creating some kind of disposable dictionary of type IDictionary<string, TValue>. For each registration of TValue, we require a name, and stick it in the dictionary. Then, when the dictionary should be disposed, it disposes all of the TValue implementations that are IDisposable.

I could envision it looking something like the following:

services.AddNamedScoped<TService, TImplementation>("Name");
services.AddNamedSingleton<TService>(typeof(TImplementation), "Name");
services.AddNamedTransient(typeof(TService), Func<IServiceProvider, TImplementation>, "Name");

Behind the scenes, this would either create or update a dictionary that had a mapping of names to services that was managed by the Microsoft service provider. When the dictionary would be disposed in accordance to its scope, it disposes its values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant