dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.65k stars 4.57k forks source link

Will property injection be revisited with the upcoming addition of required members? #76854

Open NetherGranite opened 1 year ago

NetherGranite commented 1 year ago

Is there an existing issue for this?

Is your feature request related to a problem? Please describe the problem.

Property injection is not supported in the default service container, but now required members are coming with C# 11.

Describe the solution you'd like

Property injection like in legacy ASP.NET via something like an InjectAttribute could be valuable. Maybe required members should even be automatically detected.

Additional context

I was not able to find any reasoning as to why property injection was omitted in ASP.NET Core's default service container, so I don't know if the fact that property injection was considered a design issue since it makes dependencies optional was a factor.

If it was a factor, a lot of ASP.NET Core boilerplate could be massively reduced via property injection the way required members will now reduce constructor boilerplate.

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection See info in area-owners.md if you want to be subscribed.

Issue Details
### Is there an existing issue for this? - [X] I have searched the existing issues ### Is your feature request related to a problem? Please describe the problem. Property injection is not supported in the default service container, but now required members are coming with C# 11. ### Describe the solution you'd like Property injection like in legacy ASP.NET via something like an `InjectAttribute` could be valuable. ### Additional context I was not able to find any reasoning as to why property injection was omitted in ASP.NET Core's default service container, so I don't know if the fact that property injection was considered a design issue since it makes dependencies optional was a factor. If it was a factor, a lot of ASP.NET Core boilerplate could be massively reduced via property injection the way required members will now reduce constructor boilerplate.
Author: NetherGranite
Assignees: -
Labels: `area-Extensions-DependencyInjection`
Milestone: -
tillig commented 1 year ago

This will be interesting for other container owners to follow since the conforming container may require it. If anything changes, there should be some container compatibility tests added for it.

TonyValenti commented 1 year ago

@tannergooding @SingleAccretion @jkotas @VSadov @stephentoub Hi all - Do you have any insight into future plans regarding dependency injection and required properties? AutoFac DI is considering adding support for injecting into required properties but would like to do so in a way that is compatible with any plans for Microsoft's DI. I'm an AutoFac user and would really love this feature.

davidfowl commented 1 year ago

There's currently no plan to add it, but I'd be interested in what the other DI container plan to do here (if anything).

dotnetjunkie commented 1 year ago

Simple Injector is not going to support this OOTB, but we allow users to plug this in via already existing extension points.

tillig commented 1 year ago

Autofac is adding it but there's some thought going into whether or not a property marked required yet also has nullable reference type markings indicating it can be nullable should thus be considered optional ("resolve it if you can, set null if you can't").

alistairjevans commented 1 year ago

Just to note that we just shipped v7 of Autofac with required property support; our documentation for required properties goes into the caveats and approach taken for handling various cases (e.g. where constructors and properties are mixed).

TonyValenti commented 4 months ago

Just wanted to bump this issue as work on .NET 9.0 has started.

Required property injection is an awesome feature in Autofac as it eliminates tons of boilerplate code, especially where an inheritance hierarchy exists.