-
Notifications
You must be signed in to change notification settings - Fork 10.4k
OwningComponentBase implements IAsyncDisposable #62583
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
base: main
Are you sure you want to change the base?
OwningComponentBase implements IAsyncDisposable #62583
Conversation
@dotnet-policy-service agree |
Reverted one change
Removed `OwningComponentBase.DisposeAsync()` from the PublicAPI as the implementation is private now
/// <summary> | ||
/// Releases the service scope used by the component. | ||
/// </summary> |
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.
I sense the AI working here 😄
These methods are not public, so no need to document it. In .NET when a method is an interface implementation, it can use /// <inhertidoc />
to tell it to use the same text as the interface definition.
if (disposing) | ||
{ | ||
if (_scope.HasValue) | ||
{ | ||
if (_scope.Value is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
_scope = 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.
You can probably combine the three conditions rather than nesting them:
if(disposing && _scope.HasValue && _scope.Value is IDisposable disposable)
Might be possible to also just do
if(disposing && _scope.Value is IDisposable disposable)
(without the has value check). You can set _scope = null unconditionally inside the clause
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.
Looks great!
/// Asynchronously releases the service scope used by the component. | ||
/// </summary> | ||
/// <returns>A task that represents the asynchronous dispose operation.</returns> | ||
protected virtual async ValueTask DisposeAsyncCore() |
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.
Note for API review. Since this is a "standard" implementation pattern that people might have followed within their own classes, it might be problematic if we use the same name (as the name will start conflicting) so we should consider "breaking away" from this and using a different name to "avoid the breackage".
Overall, I think this is potentially uncommon since OwningComponentBase is not common in the first place.
OwningComponentBase implements IAsyncDisposable
Description
This pull request enhances the
OwningComponentBase
class by adding support for asynchronous disposal, improving resource management, and updating tests to validate the new functionality. The most important changes include implementing theIAsyncDisposable
interface, adding new methods for asynchronous disposal, updating the public API, and introducing new test cases to ensure proper behavior.Enhancements to
OwningComponentBase
:src/Components/Components/src/OwningComponentBase.cs
: Updated theOwningComponentBase
class to implement theIAsyncDisposable
interface. AddedDisposeAsync
,DisposeAsyncCore
, and updated theDispose
method to handle both synchronous and asynchronous disposal of the service scope.Public API updates:
src/Components/Components/src/PublicAPI.Unshipped.txt
: AddedDisposeAsync
andDisposeAsyncCore
methods to the public API to reflect the new asynchronous disposal capabilities.Testing improvements:
src/Components/Components/test/OwningComponentBaseTest.cs
: Added new test cases to verify the behavior ofDisposeAsync
, ensure that accessing scoped services after disposal throws an exception.Fixes #25873