Skip to content

Conversation

@ArgoZhang
Copy link
Member

@ArgoZhang ArgoZhang commented Aug 4, 2025

Link issues

fixes #6542

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

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:

  • Add async model and field-level validation methods using ValidationMessageStore

Enhancements:

  • Implement IDisposable on BootstrapBlazorDataAnnotationsValidator to unregister validation event handlers
  • Inline registration and removal of OnValidationRequested and OnFieldChanged handlers instead of using an external extension

@bb-auto bb-auto bot added the bug Something isn't working label Aug 4, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Aug 4, 2025

Reviewer's Guide

Implements 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 BootstrapBlazorDataAnnotationsValidator

classDiagram
    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
Loading

Class diagram for removed BootstrapBlazorEditContextDataAnnotationsExtensions

classDiagram
    class BootstrapBlazorEditContextDataAnnotationsExtensions {
        <<static>>
        +void AddEditContextDataAnnotationsValidation(EditContext, ValidateForm, IServiceProvider)
    }
Loading

File-Level Changes

Change Details Files
Add IDisposable and disposal pattern to validator component
  • Implemented IDisposable on component class
  • Added Dispose(bool) and Dispose() methods
  • Unsubscribe validation events in Dispose
BootstrapBlazorDataAnnotationsValidator.cs
Refactor validation initialization to use ValidationMessageStore
  • Introduced private ValidationMessageStore field
  • Replaced legacy extension call with instantiation of ValidationMessageStore
  • Invoked new AddEditContextDataAnnotationsValidation method in OnInitialized
BootstrapBlazorDataAnnotationsValidator.cs
Extract event subscription and validation logic into separate methods
  • Created AddEditContextDataAnnotationsValidation and RemoveEditContextDataAnnotationsValidation
  • Added OnValidationRequested and OnFieldChanged handlers
  • Implemented ValidateModel and ValidateField to perform async validation
BootstrapBlazorDataAnnotationsValidator.cs
Simplify exception message formatting
  • Removed redundant namespace prefix in InvalidOperationException message
  • Streamlined string interpolation for clarity
BootstrapBlazorDataAnnotationsValidator.cs
Remove obsolete EditContext extension file
  • Deleted BootstrapBlazorEditContextDataAnnotationsExtensions.cs
BootstrapBlazorEditContextDataAnnotationsExtensions.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#6542 Implement the IDisposable interface on the BootstrapBlazorDataAnnotationsValidator component.
#6542 Ensure that resources and event handlers are properly disposed to prevent memory leaks.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto bot added this to the 9.9.0 milestone Aug 4, 2025
@ArgoZhang ArgoZhang merged commit 69ce39e into main Aug 4, 2025
5 checks passed
@ArgoZhang ArgoZhang deleted the fix-validate branch August 4, 2025 01:19
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a 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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +26 to +34
[NotNull]
private ValidateForm? ValidateForm { get; set; }

[Inject]
[NotNull]
private IServiceProvider? Provider { get; set; }

[NotNull]
private ValidationMessageStore? _message = null;
Copy link
Contributor

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);
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(BootstrapBlazorDataAnnotationsValidator): add IDispose interface

2 participants