-
-
Notifications
You must be signed in to change notification settings - Fork 362
fix(BootstrapBlazorDataAnnotationsValidator): add IDispose interface #6543
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
Conversation
Reviewer's GuideImplements IDisposable in the BootstrapBlazorDataAnnotationsValidator component, restructures data annotations validation by introducing a ValidationMessageStore, event-based validation methods, and ensures proper cleanup of subscriptions, while removing the obsolete extension file. Class diagram for updated BootstrapBlazorDataAnnotationsValidatorclassDiagram
class BootstrapBlazorDataAnnotationsValidator {
+EditContext CurrentEditContext
+ValidateForm ValidateForm
+IServiceProvider Provider
-ValidationMessageStore _message
+void OnInitialized()
+bool Validate()
-void AddEditContextDataAnnotationsValidation()
-void RemoveEditContextDataAnnotationsValidation()
-void OnValidationRequested(object? sender, ValidationRequestedEventArgs args)
-void OnFieldChanged(object? sender, FieldChangedEventArgs args)
-Task ValidateModel(EditContext editContext, ValidationMessageStore messages, IServiceProvider provider)
-Task ValidateField(EditContext editContext, ValidationMessageStore messages, FieldIdentifier field, IServiceProvider provider)
-void Dispose(bool disposing)
+void Dispose()
}
BootstrapBlazorDataAnnotationsValidator --|> ComponentBase
BootstrapBlazorDataAnnotationsValidator ..|> IDisposable
BootstrapBlazorDataAnnotationsValidator o-- ValidateForm
BootstrapBlazorDataAnnotationsValidator o-- IServiceProvider
BootstrapBlazorDataAnnotationsValidator o-- ValidationMessageStore
Class diagram for removed BootstrapBlazorEditContextDataAnnotationsExtensionsclassDiagram
class BootstrapBlazorEditContextDataAnnotationsExtensions {
<<static>>
+void AddEditContextDataAnnotationsValidation(EditContext, ValidateForm, IServiceProvider)
}
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @ArgoZhang - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `src/BootstrapBlazor/Components/ValidateForm/BootstrapBlazorDataAnnotationsValidator.cs:26` </location>
<code_context>
/// 获得/设置 当前编辑窗体上下文
/// </summary>
[CascadingParameter]
+ [NotNull]
private ValidateForm? ValidateForm { get; set; }
[Inject]
[NotNull]
private IServiceProvider? Provider { get; set; }
+ [NotNull]
+ private ValidationMessageStore? _message = null;
+
/// <summary>
</code_context>
<issue_to_address>
Initialization of _message may be redundant due to OnInitialized logic.
Since _message is always set in OnInitialized, consider making it non-nullable and initializing it there, or use late initialization to prevent null reference confusion.
Suggested implementation:
```csharp
[NotNull]
private ValidationMessageStore _message;
```
You must ensure that `_message` is always initialized in the `OnInitialized` method (or wherever the initialization logic is guaranteed to run before use). For example:
```csharp
protected override void OnInitialized()
{
base.OnInitialized();
_message = new ValidationMessageStore( /* appropriate parameters */ );
}
```
Remove any redundant initializations of `_message` elsewhere in the class.
</issue_to_address>
### Comment 2
<location> `src/BootstrapBlazor/Components/ValidateForm/BootstrapBlazorDataAnnotationsValidator.cs:70` </location>
<code_context>
+
+ private void OnValidationRequested(object? sender, ValidationRequestedEventArgs args)
+ {
+ _ = ValidateModel(CurrentEditContext, _message, Provider);
+ }
+
</code_context>
<issue_to_address>
Asynchronous validation is not awaited, which may cause race conditions.
ValidateModel and ValidateField are async but not awaited, which may lead to out-of-order validation or missed exceptions. Make the event handlers async and await these calls for correct execution.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| [NotNull] | ||
| private ValidateForm? ValidateForm { get; set; } | ||
|
|
||
| [Inject] | ||
| [NotNull] | ||
| private IServiceProvider? Provider { get; set; } | ||
|
|
||
| [NotNull] | ||
| private ValidationMessageStore? _message = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Initialization of _message may be redundant due to OnInitialized logic.
Since _message is always set in OnInitialized, consider making it non-nullable and initializing it there, or use late initialization to prevent null reference confusion.
Suggested implementation:
[NotNull]
private ValidationMessageStore _message;You must ensure that _message is always initialized in the OnInitialized method (or wherever the initialization logic is guaranteed to run before use). For example:
protected override void OnInitialized()
{
base.OnInitialized();
_message = new ValidationMessageStore( /* appropriate parameters */ );
}Remove any redundant initializations of _message elsewhere in the class.
|
|
||
| private void OnValidationRequested(object? sender, ValidationRequestedEventArgs args) | ||
| { | ||
| _ = ValidateModel(CurrentEditContext, _message, Provider); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Asynchronous validation is not awaited, which may cause race conditions.
ValidateModel and ValidateField are async but not awaited, which may lead to out-of-order validation or missed exceptions. Make the event handlers async and await these calls for correct execution.
Link issues
fixes #6542
Summary By Copilot
Regression?
Risk
Verification
Packaging changes reviewed?
☑️ Self Check before Merge
Summary by Sourcery
Enhance the BootstrapBlazorDataAnnotationsValidator by introducing ValidationMessageStore, event-driven async validation for models and fields, and implementing IDisposable to clean up event subscriptions.
New Features:
Enhancements: