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.
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 ofTValue, we require a name, and stick it in the dictionary. Then, when the dictionary should be disposed, it disposes all of theTValueimplementations that areIDisposable.I could envision it looking something like the following:
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.