-
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?
Changes from all commits
1ad90eb
8ceffaf
b6b3ddd
5bbf6b2
99ff2c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Components; | |
/// requires disposal such as a repository or database abstraction. Using <see cref="OwningComponentBase"/> | ||
/// as a base class ensures that the service provider scope is disposed with the component. | ||
/// </remarks> | ||
public abstract class OwningComponentBase : ComponentBase, IDisposable | ||
public abstract class OwningComponentBase : ComponentBase, IDisposable, IAsyncDisposable | ||
{ | ||
private AsyncServiceScope? _scope; | ||
|
||
|
@@ -44,20 +44,63 @@ protected IServiceProvider ScopedServices | |
} | ||
} | ||
|
||
/// <summary> | ||
/// Releases the service scope used by the component. | ||
/// </summary> | ||
void IDisposable.Dispose() | ||
{ | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
/// <summary> | ||
/// Releases the service scope used by the component. | ||
/// </summary> | ||
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param> | ||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!IsDisposed) | ||
{ | ||
_scope?.Dispose(); | ||
_scope = null; | ||
Dispose(disposing: true); | ||
if (disposing) | ||
{ | ||
if (_scope.HasValue) | ||
{ | ||
if (_scope.Value is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
_scope = null; | ||
} | ||
} | ||
Comment on lines
+64
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can probably combine the three conditions rather than nesting them: Might be possible to also just do |
||
IsDisposed = true; | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected virtual void Dispose(bool disposing) | ||
/// <summary> | ||
/// Asynchronously releases the service scope used by the component. | ||
/// </summary> | ||
/// <returns>A task that represents the asynchronous dispose operation.</returns> | ||
async ValueTask IAsyncDisposable.DisposeAsync() | ||
{ | ||
await DisposeAsyncCore().ConfigureAwait(false); | ||
|
||
Dispose(disposing: false); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
/// <summary> | ||
/// 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 commentThe 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. |
||
{ | ||
if (!IsDisposed && _scope.HasValue) | ||
{ | ||
await _scope.Value.DisposeAsync().ConfigureAwait(false); | ||
_scope = null; | ||
} | ||
|
||
IsDisposed = true; | ||
} | ||
} | ||
|
||
|
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.