-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjectContainerBuilder.cs
More file actions
209 lines (190 loc) · 9.9 KB
/
ObjectContainerBuilder.cs
File metadata and controls
209 lines (190 loc) · 9.9 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
// =================================================================================================================================
// 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.Linq;
namespace RapidField.SolidInstruments.ObjectComposition
{
/// <summary>
/// Represents an object that configures and initializes new <see cref="IObjectContainer" /> instances.
/// </summary>
/// <remarks>
/// <see cref="ObjectContainerBuilder" /> is the default implementation of <see cref="IObjectContainerBuilder" />.
/// </remarks>
public sealed class ObjectContainerBuilder : ObjectBuilder<IObjectContainer>, IObjectContainerBuilder
{
/// <summary>
/// Initializes a new instance of the <see cref="ObjectContainerBuilder" /> class.
/// </summary>
public ObjectContainerBuilder()
: base()
{
return;
}
/// <summary>
/// Configures the <see cref="IObjectContainer" /> to support production of <typeparamref name="TProduct" />.
/// </summary>
/// <typeparam name="TProduct">
/// The type that is produced by the container.
/// </typeparam>
/// <returns>
/// The current <see cref="ObjectContainerBuilder" />.
/// </returns>
/// <exception cref="ObjectBuilderException">
/// An exception was raised while configuring the <see cref="IObjectContainer" />. See inner exception for details.
/// </exception>
public IObjectContainerBuilder Configure<TProduct>()
where TProduct : class, new() => Configure(() => new TProduct());
/// <summary>
/// Configures the <see cref="IObjectContainer" /> to support production of <typeparamref name="TProduct" />.
/// </summary>
/// <typeparam name="TProduct">
/// The type that is produced by the container.
/// </typeparam>
/// <param name="productionFunction">
/// A function that produces the specified type.
/// </param>
/// <returns>
/// The current <see cref="ObjectContainerBuilder" />.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="productionFunction" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ObjectBuilderException">
/// An exception was raised while configuring the <see cref="IObjectContainer" />. See inner exception for details.
/// </exception>
public IObjectContainerBuilder Configure<TProduct>(Func<TProduct> productionFunction)
where TProduct : class => Configure<TProduct, TProduct>(productionFunction);
/// <summary>
/// Configures the <see cref="IObjectContainer" /> to support production of <typeparamref name="TProduct" /> in response to
/// a request for <typeparamref name="TRequest" />.
/// </summary>
/// <typeparam name="TRequest">
/// The request type that identifies the registration.
/// </typeparam>
/// <typeparam name="TProduct">
/// The type that is produced by the container as a result of a request for <typeparamref name="TRequest" />.
/// </typeparam>
/// <returns>
/// The current <see cref="ObjectContainerBuilder" />.
/// </returns>
/// <exception cref="ObjectBuilderException">
/// An exception was raised while configuring the <see cref="IObjectContainer" />. See inner exception for details.
/// </exception>
public IObjectContainerBuilder Configure<TRequest, TProduct>()
where TRequest : class
where TProduct : class, TRequest, new() => Configure<TRequest, TProduct>(() => new TProduct());
/// <summary>
/// Configures the <see cref="IObjectContainer" /> to support production of <typeparamref name="TProduct" /> in response to
/// a request for <typeparamref name="TRequest" />.
/// </summary>
/// <typeparam name="TRequest">
/// The request type that identifies the registration.
/// </typeparam>
/// <typeparam name="TProduct">
/// The type that is produced by the container as a result of a request for <typeparamref name="TRequest" />.
/// </typeparam>
/// <param name="productionFunction">
/// A function that produces the specified type.
/// </param>
/// <returns>
/// The current <see cref="ObjectContainerBuilder" />.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="productionFunction" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ObjectBuilderException">
/// An exception was raised while configuring the <see cref="IObjectContainer" />. See inner exception for details.
/// </exception>
public IObjectContainerBuilder Configure<TRequest, TProduct>(Func<TProduct> productionFunction)
where TRequest : class
where TProduct : class, TRequest
{
productionFunction.RejectIf().IsNull(nameof(productionFunction));
var requestType = typeof(TRequest);
var productType = typeof(TProduct);
var definitionKey = new ObjectContainerDefinition(requestType, productType);
try
{
if (DefinitionConfigurationActions.Keys.Any(key => key.RequestType == requestType))
{
throw new ArgumentException($"The builder is already configured for the request type {requestType.FullName}.", nameof(TRequest));
}
if (FunctionConfigurationActions.ContainsKey(definitionKey))
{
throw new ArgumentException($"The builder is already configured for the request-product type pair: {requestType.FullName}, {productType.FullName}.", nameof(TProduct));
}
DefinitionConfigurationActions.Add(definitionKey, new Action<IObjectContainerConfigurationDefinitions>((definitions) =>
{
definitions.Add<TRequest, TProduct>();
}));
if (FunctionConfigurationActions.Keys.Any(definition => definition.ProductType == productType))
{
return this;
}
FunctionConfigurationActions.Add(definitionKey, new Action<IObjectFactoryConfigurationProductionFunctions>((functions) =>
{
functions.Add(productionFunction);
}));
}
catch (ArgumentException exception)
{
throw new ObjectBuilderException(GetType(), exception);
}
return this;
}
/// <summary>
/// Releases all resources consumed by the current <see cref="ObjectContainerBuilder" />.
/// </summary>
/// <param name="disposing">
/// A value indicating whether or not managed resources should be released.
/// </param>
protected override void Dispose(Boolean disposing) => base.Dispose(disposing);
/// <summary>
/// Produces the configured <see cref="ObjectContainer" /> instance.
/// </summary>
/// <exception cref="ObjectBuilderException">
/// An exception was raised during finalization of the builder.
/// </exception>
protected sealed override IObjectContainer ToResult(IConcurrencyControlToken controlToken)
{
var factoryConfigurator = new Action<IObjectFactoryConfigurationProductionFunctions>((functions) =>
{
foreach (var action in FunctionConfigurationActions.Values)
{
action(functions);
}
});
var definitionConfigurator = new Action<IObjectContainerConfigurationDefinitions>((definitions) =>
{
foreach (var action in DefinitionConfigurationActions.Values)
{
action(definitions);
}
});
return new ObjectContainer(factoryConfigurator, definitionConfigurator);
}
/// <summary>
/// Represents a collection of actions that configure the definitions that control the behavior of the resulting
/// <see cref="IObjectContainer" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IDictionary<IObjectContainerDefinition, Action<IObjectContainerConfigurationDefinitions>> DefinitionConfigurationActions = new Dictionary<IObjectContainerDefinition, Action<IObjectContainerConfigurationDefinitions>>();
/// <summary>
/// Represents a collection of definitions that control the behavior of the resulting <see cref="IObjectContainer" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IObjectContainerConfigurationDefinitions Definitions = new ObjectContainerConfigurationDefinitions();
/// <summary>
/// Represents a collection of actions that configure the functions that produce new object instances for the resulting
/// <see cref="IObjectContainer" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IDictionary<IObjectContainerDefinition, Action<IObjectFactoryConfigurationProductionFunctions>> FunctionConfigurationActions = new Dictionary<IObjectContainerDefinition, Action<IObjectFactoryConfigurationProductionFunctions>>();
}
}