-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFactoryProducedInstanceGroup.cs
More file actions
219 lines (205 loc) · 8.57 KB
/
FactoryProducedInstanceGroup.cs
File metadata and controls
219 lines (205 loc) · 8.57 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
// =================================================================================================================================
// 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.Concurrency;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace RapidField.SolidInstruments.ObjectComposition
{
/// <summary>
/// Manages object creation, storage, resolution and disposal for a related group of factory-produced object instances.
/// </summary>
/// <remarks>
/// <see cref="FactoryProducedInstanceGroup" /> is the default implementation of <see cref="IFactoryProducedInstanceGroup" />.
/// </remarks>
public class FactoryProducedInstanceGroup : Instrument, IFactoryProducedInstanceGroup
{
/// <summary>
/// Initializes a new instance of the <see cref="FactoryProducedInstanceGroup" /> class.
/// </summary>
/// <param name="factory">
/// A factory that creates new object instances for the group.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="factory" /> is <see langword="null" />.
/// </exception>
public FactoryProducedInstanceGroup(IObjectFactory factory)
: base()
{
Factory = factory.RejectIf().IsNull(nameof(factory)).TargetArgument;
Instances = new Dictionary<Type, Object>();
}
/// <summary>
/// Returns the instance of specified type that is managed by the current <see cref="FactoryProducedInstanceGroup" />.
/// </summary>
/// <typeparam name="T">
/// The type of the instance to return.
/// </typeparam>
/// <returns>
/// The instance of specified type that is managed by the current <see cref="FactoryProducedInstanceGroup" />.
/// </returns>
/// <exception cref="ArgumentException">
/// <typeparamref name="T" /> is not a supported type for the group.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
/// <exception cref="ObjectProductionException">
/// An exception was raised during object production.
/// </exception>
public T Get<T>()
where T : class
{
var instanceType = typeof(T);
using (var controlToken = StateControl.Enter())
{
RejectIfDisposed();
if (Instances.TryGetValue(instanceType, out var extantInstance))
{
return (extantInstance as T);
}
var newInstance = GetNew<T>(controlToken);
Instances.Add(instanceType, newInstance);
return newInstance;
}
}
/// <summary>
/// Returns the lazily-initialized instance of specified type that is managed by the current
/// <see cref="FactoryProducedInstanceGroup" />.
/// </summary>
/// <typeparam name="T">
/// The type of the lazily-initialized instance to return.
/// </typeparam>
/// <returns>
/// The lazily-initialized instance of specified type that is managed by the current
/// <see cref="FactoryProducedInstanceGroup" />.
/// </returns>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
public Lazy<T> GetLazy<T>()
where T : class
{
RejectIfDisposed();
return new Lazy<T>(() => Get<T>(), LazyThreadSafetyMode.PublicationOnly);
}
/// <summary>
/// Returns a new instance of specified type that is managed by the current <see cref="FactoryProducedInstanceGroup" />.
/// </summary>
/// <remarks>
/// <see cref="GetNew{T}()" /> differs from <see cref="Get{T}" /> in that it returns a distinct instance for every
/// subsequent call.
/// </remarks>
/// <typeparam name="T">
/// The type of the instance to return.
/// </typeparam>
/// <returns>
/// The instance of specified type that is managed by the current <see cref="FactoryProducedInstanceGroup" />.
/// </returns>
/// <exception cref="ArgumentException">
/// <typeparamref name="T" /> is not a supported type for the group.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
/// <exception cref="ObjectProductionException">
/// An exception was raised during object production.
/// </exception>
public T GetNew<T>()
where T : class
{
using (var controlToken = StateControl.Enter())
{
RejectIfDisposed();
return GetNew<T>(controlToken);
}
}
/// <summary>
/// Releases all resources consumed by the current <see cref="FactoryProducedInstanceGroup" />.
/// </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)
{
ReferenceManager.Dispose();
}
}
finally
{
base.Dispose(disposing);
}
}
/// <summary>
/// Returns the instance of specified type that is managed by the current <see cref="FactoryProducedInstanceGroup" />.
/// </summary>
/// <typeparam name="T">
/// The type of the instance to return.
/// </typeparam>
/// <param name="controlToken">
/// A token that represents and manages contextual thread safety.
/// </param>
/// <returns>
/// The instance of specified type that is managed by the current <see cref="FactoryProducedInstanceGroup" />.
/// </returns>
/// <exception cref="ArgumentException">
/// <typeparamref name="T" /> is not a supported type for the group.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
/// <exception cref="ObjectProductionException">
/// An exception was raised during object production.
/// </exception>
[DebuggerHidden]
private T GetNew<T>(IConcurrencyControlToken controlToken)
where T : class
{
var instanceType = typeof(T);
var newInstance = Factory.Produce(instanceType) as T;
ReferenceManager.AddObject(newInstance);
return newInstance;
}
/// <summary>
/// Gets the types of the instances that are managed by the current <see cref="FactoryProducedInstanceGroup" />.
/// </summary>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
public IEnumerable<Type> InstanceTypes
{
get
{
foreach (var supportedProductType in Factory.SupportedProductTypes)
{
RejectIfDisposed();
yield return supportedProductType;
}
}
}
/// <summary>
/// Represents a factory that creates new object instances for the group.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IObjectFactory Factory;
/// <summary>
/// Represents the instances that are managed by the current <see cref="FactoryProducedInstanceGroup" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IDictionary<Type, Object> Instances;
/// <summary>
/// Represents a utility that disposes of the object references that are managed by the current
/// <see cref="FactoryProducedInstanceGroup" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IReferenceManager ReferenceManager = new ReferenceManager();
}
}