Open Danielku15 opened 2 months ago
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection See info in area-owners.md if you want to be subscribed.
Very strongly against this proposal as it just pushes people into the pit of failure. Property injection is an abomination that should never be used unless one is dealing with a super legacy framework that does not support proper constructor injection (even WebForms supports ctor-based injection now, so I don't even have an example anymore).
The example's IServiceInit
interface is especially egregious as it goes against the correctness-by-construction principle and introduces temporal/sequential coupling to whoever uses it.
I hope this does not move forward.
@julealgon I'm hiding this reply behind an expander as it is rather a direct counter-statement to your opinion than adding much value to the general proposal discussion. I'd like to emphasize some values of the .net foundation code of conduct:
Different opinions count, same as yours. I think an API proposal like this is not the right forum to discuss architectural design opinions. There is a good reason this proposal aims to open for extension and not extend the main library. If somebody decides to allow aspects in their architectures its their choice. I see this from an "open-closed priniciple" standpoint and this proposal aims to allow easier extension without exchanging the whole DI container. The extensions are the legit choice of developers authoring their software.
Background and motivation
Architectures and principles can differ from project to project and when it comes to centralizing some aspects of your services the DI container is often a first point to handle these aspects.
While I can understand that the extensions team doesn't want to add a central abstraction and implementation for systems like property injection ^1^3, I cannot fully support this decision. Microsoft libraries often rather try to be not "opinionated" but it seems in this case we are lacking the right extension point for developers to add support for such a mechanism without bloating the core. It seems like an extreme measure to fully swap out the DI container because some additional customization aspects are needed.
So I was thinking of an alternative approach to tackle this topic: What if the default DI container would provide an extension point where developers or library authors can hook into the service creation? Such a callback would open the door for a nice range of extensions:
This is a similar strategy like EF Core^5 follows. With interceptors the main library can be augmented and extended with new aspects without fully exchanging central bits (like the requiring to exchange the provider).
API Proposal
I am not fully sure about the API design itself, I'm open for any alternative approach providing the same result.
As main API proposal I would see: Add an option for a callback which the DI container calls when a service was realized.
API Usage
Alternative Designs
Ad 1: The
ServiceProviderEngine
and default implementation could be made public or be exposed with another abstraction. I don't like this approach so much as it pulls quite many internals but its still a legit one. It might make it more complicated for others to extend the default behavior as the whole engine needs to be substituted.Ad 2: Instead of a simple delegate we could also define an interface and allow multiple "interceptors" to be registered. This would allow an easier extension through extension methods and external libraries:
While from an API design it might be better for extension, it has more performance and complexity implications (order of interceptors, performance with looping and interface calls, chicken-egg problem if people want to use DI already in the interceptors).
Risks
The DI container is a perfromance sensitive aspect in most applications. Poorly written interceptors could result in a performance degredation which is percieved as bad performance of the DI framework itself.
Unexperienced users might not understand that misbehaviors might be caused by interceptors resulting in additional issues being raised against the core library while 3rd party extensions are to blame. With the .net hostbuilders its easy to hide things behind the scenes.