Skip to content
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

Enable Razor Components constructor injection #20776

Closed
weitzhandler opened this issue Apr 12, 2020 · 2 comments
Closed

Enable Razor Components constructor injection #20776

weitzhandler opened this issue Apr 12, 2020 · 2 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@weitzhandler
Copy link

weitzhandler commented Apr 12, 2020

Hi,

The current method of DI in Razor Components is by decorating the target properties with TargetAttribute.

This is not the traditional method of injection using the constructor, and in addition, makes it uncertain for the compiler whether these fields are gonna be null.

When using null reference types of C# 8, the compiler will issue a warning for all those fields, marking them nullable (?), they will issue warnings in the UI side of the page, as Razor doesn't seem to support the null conditional operator (?. or ?[]).

One solution could be suppressing the warnings manually or on top of page, like:

[Inject]
[SuppressMessage("Compiler", "CS8618")]
protected AppDbContext DbContext { get; set; }

But the only solution that worked for me was to disable #nullable manually in each Razor page with injected properties.

My suggestion is that you extend the Razor generator so that it generate non-parameterless constructors in the same signature as the base-class code-behind.

cc: @guardrex.

@javiercn
Copy link
Member

@weitzhandler thanks for contacting us.

Closing this issue as it is a dupe of #18088. we'll be using that issue to track it.

@weitzhandler
Copy link
Author

Self reference: another alternative is to set the property to default! or null!.

@ghost ghost locked as resolved and limited conversation to collaborators May 13, 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
Projects
None yet
Development

No branches or pull requests

3 participants