-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
Constructor injection for Blazor components #18088
Comments
Thanks for your issue report. We'll consider whether to address this during our next milestone planning. |
Thanks for that |
This is a must have. Property based injection prevents us from consuming them within the constructor there by preventing us from making some (other) fields read only. |
Comment from a duplicate issue I opened: Currently blazor only allows injecting services into components using property injection via the However this has a number of disadvantages over constructor injection
ProposalIf there is a single constructor, use it for dependency injection. It is an error if any of the parameters cannot be resolved. It is an error if there are multiple constructors, none of which are parameterless, unless one of them is marked with |
|
Any updates on this? @pranavkm |
Moving to backlog given this now be achieved using |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
@mkArtakMSFT how can it be achieved using IComponentActivator? Could you please give an example? |
That is now unblocked. There is still perf research\work to do here on the DI side, but the size regression is no longer blocked. There is now a reflection path that doesn't use LINQ. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Thanks for contacting us. We're moving this issue to the |
Looks like this is going to make it into .NET 9, which is great! In the meantime, items 1, and 3 can be addressed by marking the service properties 'public required' and e.g. use an 'init' setter. Although not ideal, it might be preferable to suppressing the nullability warnings |
Yes, that is the best workaround. I was setting the properties to "null!" (notice the exclamation mark) until I discovered the "required" keyword. |
Is your feature request related to a problem? Please describe.
Constructor injection in Components was ruled out previously as "we don't plan to do this" but now that we have partials is there any change in this attitude?
see #15779 , #5497
Describe the solution you'd like
If Blazor could be modified to optionally use DI to source components, then constructor injection would be possible,
If you would be open to at least reviewing a PR, I'm sure the community would do the groundwork to make it happen.
The text was updated successfully, but these errors were encountered: