forked from sungam3r/SteroidsDI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scoped.cs
112 lines (98 loc) · 4.23 KB
/
Scoped.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System.Reflection;
namespace SteroidsDI.Core;
/// <summary>
/// Auxiliary class for creating and destroying scopes. Scopes are sections of code within which
/// scoped dependencies are resolved. This class controls the state of <see cref="GenericScope{T}" />,
/// initializing its initial state and cleaning/destroying it "at the exit", i.e when calling
/// <see cref="Dispose"/> or <see cref="DisposeAsync"/>.
/// </summary>
/// <typeparam name="T">
/// An arbitrary type that is used to create various static AsyncLocal fields. The caller may
/// set unique closed type, thereby providing its own storage, to which only he will have access.
/// </typeparam>
public readonly struct Scoped<T> : IDisposable, IAsyncDisposable
{
/// <summary>
/// Initializes a new instance of the <see cref="Scoped{T}"/>.
/// </summary>
/// <param name="scopeFactory"> A factory for creating scopes. </param>
public Scoped(IScopeFactory scopeFactory)
{
if (scopeFactory == null)
throw new ArgumentNullException(nameof(scopeFactory));
if (GenericScope<T>.CurrentScope != null)
throw new InvalidOperationException($"The current scope of GenericScope<{typeof(T).Name}> is not null when trying to initialize it.");
Scope = scopeFactory.CreateScope();
GenericScope<T>.CurrentScope = Scope;
}
/// <summary>
/// Gets current scope.
/// </summary>
public IDisposable Scope { get; }
/// <inheritdoc />
public void Dispose()
{
GenericScope<T>.CurrentScope = null;
Scope?.Dispose(); // in some cases scopeFactory.CreateScope() MAY return null
}
/// <inheritdoc />
public ValueTask DisposeAsync()
{
GenericScope<T>.CurrentScope = null;
if (Scope is IAsyncDisposable ad)
return ad.DisposeAsync();
Scope?.Dispose(); // in some cases scopeFactory.CreateScope() MAY return null
// ValueTask.CompletedTask is only available in net5.0 and later.
return default;
}
}
/// <summary>
/// Auxiliary class for creating and destroying scopes. Scopes are sections of code within which
/// scoped dependencies are resolved. This class controls the state of <see cref="GenericScope{T}" />,
/// initializing its initial state and cleaning/destroying it "at the exit", i.e when calling
/// <see cref="Dispose"/> or <see cref="DisposeAsync"/>.
/// <br/><br/>
/// Non-generic version of <see cref="Scoped{T}"/>.
/// </summary>
public readonly struct Scoped : IDisposable, IAsyncDisposable
{
private readonly PropertyInfo _currentScopeProperty;
/// <summary>
/// Initializes a new instance of the <see cref="Scoped"/>.
/// </summary>
/// <param name="type">
/// An arbitrary type that is used to create various static AsyncLocal fields. The caller may
/// set unique closed type, thereby providing its own storage, to which only he will have access.
/// </param>
/// <param name="scopeFactory"> A factory for creating scopes. </param>
public Scoped(Type type, IScopeFactory scopeFactory)
{
if (scopeFactory == null)
throw new ArgumentNullException(nameof(scopeFactory));
_currentScopeProperty = typeof(GenericScope<>).MakeGenericType(type).GetProperty(nameof(GenericScope<int>.CurrentScope));
if (_currentScopeProperty.GetValue(null) != null)
throw new InvalidOperationException($"The current scope of GenericScope<{type.Name}> is not null when trying to initialize it.");
Scope = scopeFactory.CreateScope();
_currentScopeProperty.SetValue(null, Scope);
}
/// <summary>
/// Gets current scope.
/// </summary>
public IDisposable Scope { get; }
/// <inheritdoc />
public void Dispose()
{
_currentScopeProperty.SetValue(null, null);
Scope?.Dispose(); // in some cases scopeFactory.CreateScope() MAY return null
}
/// <inheritdoc />
public ValueTask DisposeAsync()
{
_currentScopeProperty.SetValue(null, null);
if (Scope is IAsyncDisposable ad)
return ad.DisposeAsync();
Scope?.Dispose(); // in some cases scopeFactory.CreateScope() MAY return null
// ValueTask.CompletedTask is only available in net5.0 and later.
return default;
}
}