Skip to content

Commit 1e7db27

Browse files
committed
Updates
1 parent a778be5 commit 1e7db27

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

aspnetcore/diagnostics/bl0008.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@ The analyzer warns developers when this pattern is detected, while safely ignori
2828
Consider the following `Input` form model with a property initializer:
2929

3030
```csharp
31-
public class MyComponent : ComponentBase
32-
{
33-
[SupplyParameterFromForm]
34-
public InputModel Input { get; set; } = new InputModel();
35-
}
31+
[SupplyParameterFromForm]
32+
public InputModel Input { get; set; } = new();
3633
```
3734

3835
The analyzer reports the following warning:
@@ -53,36 +50,36 @@ public InputModel Input { get; set; } = null!;
5350

5451
To ensure the initialized value isn't overwritten, move the initialization to one of the [`OnInitialized{Async}` lifecycle methods](xref:blazor/components/lifecycle#component-initialization-oninitializedasync).
5552

56-
Consider the following Razor component that generates a BL0008 code analysis warning because the `Model` for the form is decorated with `[SupplyParameterFromForm]` and has a property initializer:
53+
Consider the following Razor component that generates a BL0008 code analysis warning because the `Input` model for the form is decorated with `[SupplyParameterFromForm]` and has a property initializer:
5754

5855
```razor
59-
<EditForm Model="Model" OnSubmit="Submit" FormName="Starship1">
56+
<EditForm Model="Input" OnSubmit="Submit" FormName="Starship1">
6057
...
6158
</EditForm>
6259
6360
@code {
6461
[SupplyParameterFromForm]
65-
private Starship Model { get; set; } = new();
62+
private InputModel Input { get; set; } = new();
6663
6764
private void Submit()
6865
{
6966
...
7067
}
7168
72-
public class Starship
69+
public class InputModel
7370
{
7471
public string? Id { get; set; }
7572
}
7673
}
7774
```
7875

79-
To fix the violation, the initialization code for the `Model` property is moved to the [`OnInitialized` lifecycle method](xref:blazor/components/lifecycle#component-initialization-oninitializedasync), leaving the property as an [automatically implemented property (*auto property*)](/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties). The following change also makes `Model` a [nullable reference types (NRT)](xref:migration/50-to-60#nullable-reference-types-nrts-and-net-compiler-null-state-static-analysis):
76+
To fix the violation, the initialization code for the `Input` property is moved to the [`OnInitialized` lifecycle method](xref:blazor/components/lifecycle#component-initialization-oninitializedasync), leaving the property as an [automatically implemented property (*auto property*)](/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties). The following change also makes `Input` a [nullable reference types (NRT)](xref:migration/50-to-60#nullable-reference-types-nrts-and-net-compiler-null-state-static-analysis):
8077

8178
```csharp
8279
[SupplyParameterFromForm]
83-
private Starship? Model { get; set; }
80+
private InputModel? Input { get; set; }
8481

85-
protected override void OnInitialized() => Model ??= new();
82+
protected override void OnInitialized() => Input ??= new();
8683
```
8784

8885
## When to suppress warnings

aspnetcore/diagnostics/code-analysis.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Diagnostic ID:
5050
* [BL0005](xref:diagnostics/bl0005)
5151
* [BL0006](xref:diagnostics/bl0006)
5252
* [BL0007](xref:diagnostics/bl0007)
53+
* [BL0008](xref:diagnostics/bl0008)
5354
* [MVC1000](xref:diagnostics/mvc1000)
5455
* [MVC1001](xref:diagnostics/mvc1001)
5556
* [MVC1002](xref:diagnostics/mvc1002)

0 commit comments

Comments
 (0)