Injected properties are expected to be available#24685
Conversation
| ``` | ||
|
|
||
| > [!NOTE] | ||
| > Don't mark injected services as nullable. Instead, assign a default literal with the null-forgiving operator (`default!`). For example: |
There was a problem hiding this comment.
I guess. One of the alternatives we were considering was to enable constructor injection by default in 7.0 which would remove some of this ugliness.
There was a problem hiding this comment.
Sorry for the nitpick, but I think it would be best to clarify as to why you shouldn't mark properties as nullable:
Since injected properties are always expected to be available, as a best practice don't mark injected services as nullable. Instead...
The first time I read this, I thought that there might be some more technical reason as to why you shouldn't mark it as nullable.
There was a problem hiding this comment.
It's not just a best practice from the remarks made: It's how Blazor is designed to work. Therefore, an imperative remark should be fine.
I do see one thing that I don't care for ... "My"-named things 😩 ... I think I'll patch that out of here. I'll add that bit, @jessegood, when I make that update.
There was a problem hiding this comment.
I see, so as a consumer, we shouldn't think of this as being "optional" at all. Thanks for the clarification!
Co-authored-by: Pranav K <prkrishn@hotmail.com>
Addresses #24615
Per dotnet/aspnetcore#39445 (comment) ...
Pranav, keep/update this? ... or close the PR (e.g., it's too weedy ... too rare)? 👂
Currently, this is only for the 6.0 or later versions due to NRT enforcement for C# 8.0 or later. If we keep/modify this, let me know if you feel that 3.1/5.0 should have this guidance as well. 👂