Skip to content

Allow constructor parameter injection for components #22209

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

Closed
YairHalberstadt opened this issue May 26, 2020 · 1 comment
Closed

Allow constructor parameter injection for components #22209

YairHalberstadt opened this issue May 26, 2020 · 1 comment
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved
Milestone

Comments

@YairHalberstadt
Copy link

Currently blazor only allows injecting services into components using property injection via the [Inject] attribute.

However this has a number of disadvantages over constructor injection

  1. NRTs
    The compiler warns if you declare you service as non-nullable, and nullable reference types are enabled. You have to manually suppress the warning.
  2. Immutablility
    You cannot declare the services readonly, since they need to be set by the injector.
  3. Manual construction
    If you create the component manually (perhaps for unit testing), there is nothing to guarentee you will set the services. Even worse - you often can't since the properties are usually private. You have to declare two constructors - one parameterless, and one which sets all the services. This is needless code duplication, and means that test and app code go through different paths, which is undesirable.
  4. Validation
    The constructor is a place where you can validate all your services, and do transformations on them. Perhaps, you don't want to store your services, but just call one service with another service, and store the result. Constructors are a good place to do that, with property injection this is tricky.

Proposal

If 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 [Inject]. After the construction, property injection takes place.

@javiercn javiercn added the area-blazor Includes: Blazor, Razor Components label May 26, 2020
@mkArtakMSFT mkArtakMSFT added the enhancement This issue represents an ask for new feature or an enhancement to an existing one label May 26, 2020
@mkArtakMSFT mkArtakMSFT added this to the Next sprint planning milestone May 26, 2020
@mkArtakMSFT
Copy link
Contributor

Thanks for contacting us. This is a dupe of #18088

@mkArtakMSFT mkArtakMSFT added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label May 26, 2020
@ghost ghost added the Status: Resolved label May 26, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Jun 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants