-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
OwningComponentBase.cs
92 lines (79 loc) · 3.26 KB
/
OwningComponentBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Components;
/// <summary>
/// A base class that creates a service provider scope.
/// </summary>
/// <remarks>
/// Use the <see cref="OwningComponentBase"/> class as a base class to author components that control
/// the lifetime of a service provider scope. This is useful when using a transient or scoped service that
/// 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
{
private AsyncServiceScope? _scope;
[Inject] IServiceScopeFactory ScopeFactory { get; set; } = default!;
/// <summary>
/// Gets a value determining if the component and associated services have been disposed.
/// </summary>
protected bool IsDisposed { get; private set; }
/// <summary>
/// Gets the scoped <see cref="IServiceProvider"/> that is associated with this component.
/// </summary>
protected IServiceProvider ScopedServices
{
get
{
if (ScopeFactory == null)
{
throw new InvalidOperationException("Services cannot be accessed before the component is initialized.");
}
ObjectDisposedException.ThrowIf(IsDisposed, this);
_scope ??= ScopeFactory.CreateAsyncScope();
return _scope.Value.ServiceProvider;
}
}
void IDisposable.Dispose()
{
if (!IsDisposed)
{
_scope?.Dispose();
_scope = null;
Dispose(disposing: true);
IsDisposed = true;
}
}
/// <inheritdoc />
protected virtual void Dispose(bool disposing)
{
}
}
/// <summary>
/// A base class that creates a service provider scope, and resolves a service of type <typeparamref name="TService"/>.
/// </summary>
/// <typeparam name="TService">The service type.</typeparam>
/// <remarks>
/// Use the <see cref="OwningComponentBase{TService}"/> class as a base class to author components that control
/// the lifetime of a service or multiple services. This is useful when using a transient or scoped service that
/// requires disposal such as a repository or database abstraction. Using <see cref="OwningComponentBase{TService}"/>
/// as a base class ensures that the service and relates services that share its scope are disposed with the component.
/// </remarks>
public abstract class OwningComponentBase<TService> : OwningComponentBase, IDisposable where TService : notnull
{
private TService _item = default!;
/// <summary>
/// Gets the <typeparamref name="TService"/> that is associated with this component.
/// </summary>
protected TService Service
{
get
{
ObjectDisposedException.ThrowIf(IsDisposed, this);
// We cache this because we don't know the lifetime. We have to assume that it could be transient.
_item ??= ScopedServices.GetRequiredService<TService>();
return _item;
}
}
}