-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjectFactoryConfigurationProductionFunctions.cs
More file actions
117 lines (108 loc) · 5.22 KB
/
ObjectFactoryConfigurationProductionFunctions.cs
File metadata and controls
117 lines (108 loc) · 5.22 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
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using RapidField.SolidInstruments.Core.ArgumentValidation;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
namespace RapidField.SolidInstruments.ObjectComposition
{
/// <summary>
/// Represents a collection of configured types and functions that produce them using an <see cref="ObjectFactory" />.
/// </summary>
/// <remarks>
/// <see cref="ObjectFactoryConfigurationProductionFunctions" /> is the default implementation of
/// <see cref="IObjectFactoryConfigurationProductionFunctions" />.
/// </remarks>
public sealed class ObjectFactoryConfigurationProductionFunctions : ObjectFactoryConfigurationProductionFunctions<Object>, IObjectFactoryConfigurationProductionFunctions
{
/// <summary>
/// Initializes a new instance of the <see cref="ObjectFactoryConfigurationProductionFunctions" /> class.
/// </summary>
[DebuggerHidden]
internal ObjectFactoryConfigurationProductionFunctions()
: base()
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="ObjectFactoryConfiguration" /> class.
/// </summary>
/// <param name="functions">
/// Configured types that are wrapped by the new functions.
/// </param>
[DebuggerHidden]
internal ObjectFactoryConfigurationProductionFunctions(IObjectFactoryConfigurationProductionFunctions<Object> functions)
: base(((ObjectFactoryConfigurationProductionFunctions<Object>)functions).Dictionary)
{
return;
}
}
/// <summary>
/// Represents a collection of configured types and functions that produce them using an
/// <see cref="ObjectFactory{TProductBase}" />.
/// </summary>
/// <remarks>
/// <see cref="ObjectFactoryConfigurationProductionFunctions{TProductBase}" /> is the default implementation of
/// <see cref="IObjectFactoryConfigurationProductionFunctions{TProductBase}" />.
/// </remarks>
/// <typeparam name="TProductBase">
/// The base type from which objects produced by the associated factory derive.
/// </typeparam>
public class ObjectFactoryConfigurationProductionFunctions<TProductBase> : IObjectFactoryConfigurationProductionFunctions<TProductBase>
{
/// <summary>
/// Initializes a new instance of the <see cref="ObjectFactoryConfigurationProductionFunctions{TProductBase}" /> class.
/// </summary>
[DebuggerHidden]
internal ObjectFactoryConfigurationProductionFunctions()
: this(new ConcurrentDictionary<Type, IObjectFactoryProductionFunction>())
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="ObjectFactoryConfigurationProductionFunctions{TProductBase}" /> class.
/// </summary>
/// <param name="dictionary">
/// A collection of functions that produce an output.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="dictionary" /> is <see langword="null" />.
/// </exception>
[DebuggerHidden]
internal ObjectFactoryConfigurationProductionFunctions(ConcurrentDictionary<Type, IObjectFactoryProductionFunction> dictionary)
{
Dictionary = dictionary.RejectIf().IsNull(nameof(dictionary));
}
/// <summary>
/// Defines the function that produces the specified type.
/// </summary>
/// <typeparam name="TProduct">
/// The type of the object produced by the function.
/// </typeparam>
/// <param name="function">
/// A function that produces an output of type <typeparamref name="TProduct" />.
/// </param>
/// <exception cref="ArgumentException">
/// A function is already defined for the specified type, <typeparamref name="TProduct" />.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="function" /> is <see langword="null" />.
/// </exception>
public IObjectFactoryConfigurationProductionFunctions<TProductBase> Add<TProduct>(Func<TProduct> function)
where TProduct : class, TProductBase
{
var productType = typeof(TProduct);
if (Dictionary.TryAdd(productType, new ObjectFactoryProductionFunction<TProduct>(function)))
{
return this;
}
throw new ArgumentException($"A function is already defined for the specified type, {productType.FullName}.", nameof(function));
}
/// <summary>
/// Represents a collection of functions that produce an output.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal readonly ConcurrentDictionary<Type, IObjectFactoryProductionFunction> Dictionary;
}
}