-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDependencyScope.cs
More file actions
244 lines (223 loc) · 9.39 KB
/
DependencyScope.cs
File metadata and controls
244 lines (223 loc) · 9.39 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using RapidField.SolidInstruments.Core;
using RapidField.SolidInstruments.Core.ArgumentValidation;
using RapidField.SolidInstruments.Core.Extensions;
using System;
using System.Diagnostics;
using System.Threading;
namespace RapidField.SolidInstruments.InversionOfControl
{
/// <summary>
/// Represents a shared initialization and disposal scope for container-resolved objects.
/// </summary>
/// <remarks>
/// <see cref="DependencyScope{TScope}" /> is the default implementation of <see cref="IDependencyScope" />.
/// </remarks>
/// <typeparam name="TScope">
/// The type of the scope that is abstracted by the <see cref="DependencyScope{TScope}" />.
/// </typeparam>
public abstract class DependencyScope<TScope> : Instrument, IDependencyScope
where TScope : class, IDisposable
{
/// <summary>
/// Initializes a new instance of the <see cref="DependencyScope{TScope}" /> class.
/// </summary>
/// <param name="sourceScope">
/// The underlying scope.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="sourceScope" /> is <see langword="null" />.
/// </exception>
protected DependencyScope(TScope sourceScope)
: base()
{
LazyReferenceManager = new Lazy<IReferenceManager>(() => new ReferenceManager(), LazyThreadSafetyMode.ExecutionAndPublication);
SourceScope = sourceScope.RejectIf().IsNull(nameof(sourceScope));
}
/// <summary>
/// Creates a new child initialization and disposal scope for the current <see cref="DependencyScope{TScope}" />.
/// </summary>
/// <returns>
/// A new child initialization and disposal scope for the current <see cref="DependencyScope{TScope}" />.
/// </returns>
/// <exception cref="CreateDependencyScopeException">
/// An exception was raised while attempting to create the scope.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
public IDependencyScope CreateChildScope()
{
RejectIfDisposed();
try
{
var childScope = CreateChildScope(SourceScope);
ReferenceManager.AddObject(childScope);
return childScope;
}
catch (Exception exception)
{
throw new CreateDependencyScopeException(exception);
}
}
/// <summary>
/// Requests an object of specified type from the associated container for the current <see cref="IDependencyScope" />.
/// </summary>
/// <param name="type">
/// The type of the object to resolve.
/// </param>
/// <returns>
/// An object of specified type from the associated container.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="type" /> is <see langword="null" />.
/// </exception>
/// <exception cref="DependencyResolutionException">
/// An exception was raised while attempting to resolve the dependency.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
public Object Resolve(Type type)
{
RejectIfDisposed();
try
{
var resolvedObject = Resolve(SourceScope, type.RejectIf().IsNull(nameof(type)));
if (ManagesReferencesForSourceScope)
{
ReferenceManager.AddObject(resolvedObject);
}
return resolvedObject;
}
catch (Exception exception)
{
throw new DependencyResolutionException(type, exception);
}
}
/// <summary>
/// Requests an object of specified type from the associated container for the current
/// <see cref="DependencyScope{TScope}" />.
/// </summary>
/// <typeparam name="T">
/// The type of the object to resolve.
/// </typeparam>
/// <returns>
/// An object of specified type from the associated container.
/// </returns>
/// <exception cref="DependencyResolutionException">
/// An exception was raised while attempting to resolve the dependency.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
public T Resolve<T>()
where T : class
{
RejectIfDisposed();
try
{
var resolvedObject = Resolve<T>(SourceScope);
if (ManagesReferencesForSourceScope)
{
ReferenceManager.AddObject(resolvedObject);
}
return resolvedObject;
}
catch (Exception exception)
{
throw new DependencyResolutionException(typeof(T), exception);
}
}
/// <summary>
/// Creates a new child initialization and disposal scope for the current <see cref="DependencyScope{TScope}" />.
/// </summary>
/// <param name="sourceScope">
/// The underlying scope.
/// </param>
/// <returns>
/// A new child initialization and disposal scope for the current <see cref="DependencyScope{TScope}" />.
/// </returns>
protected abstract IDependencyScope CreateChildScope(TScope sourceScope);
/// <summary>
/// Releases all resources consumed by the current <see cref="DependencyScope{TScope}" />.
/// </summary>
/// <param name="disposing">
/// A value indicating whether or not managed resources should be released.
/// </param>
protected override void Dispose(Boolean disposing)
{
try
{
if (disposing)
{
SourceScope.Dispose();
LazyReferenceManager.Dispose();
}
}
finally
{
base.Dispose(disposing);
}
}
/// <summary>
/// Requests an object of specified type from the associated container for the current
/// <see cref="DependencyScope{TScope}" />.
/// </summary>
/// <param name="sourceScope">
/// The underlying scope.
/// </param>
/// <param name="type">
/// The type of the object to resolve.
/// </param>
/// <returns>
/// An object of specified type from the associated container.
/// </returns>
protected abstract Object Resolve(TScope sourceScope, Type type);
/// <summary>
/// Requests an object of specified type from the associated container for the current
/// <see cref="DependencyScope{TScope}" />.
/// </summary>
/// <param name="sourceScope">
/// The underlying scope.
/// </param>
/// <typeparam name="T">
/// The type of the object to resolve.
/// </typeparam>
/// <returns>
/// An object of specified type from the associated container.
/// </returns>
protected abstract T Resolve<T>(TScope sourceScope)
where T : class;
/// <summary>
/// Gets a value indicating whether or not the current <see cref="DependencyScope{TScope}" /> manages and disposes of
/// resolved references on behalf of the scope.
/// </summary>
protected virtual Boolean ManagesReferencesForSourceScope => DefaultManagesReferencesForSourceScope;
/// <summary>
/// Gets a utility that disposes of the object references that are managed by the current
/// <see cref="DependencyScope{TScope}" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private IReferenceManager ReferenceManager => LazyReferenceManager.Value;
/// <summary>
/// Represents the default value indicating whether or not the current <see cref="DependencyScope{TScope}" /> manages and
/// disposes of resolved references on behalf of the source scope.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const Boolean DefaultManagesReferencesForSourceScope = false;
/// <summary>
/// Represents the lazily-initialized utility that disposes of the object references that are managed by the current
/// <see cref="DependencyScope{TScope}" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Lazy<IReferenceManager> LazyReferenceManager;
/// <summary>
/// Gets the scope that is abstracted by the current <see cref="DependencyScope{TScope}" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly TScope SourceScope;
}
}