-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfigurableInstrument.cs
More file actions
155 lines (141 loc) · 6.67 KB
/
ConfigurableInstrument.cs
File metadata and controls
155 lines (141 loc) · 6.67 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
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using Microsoft.Extensions.Configuration;
using RapidField.SolidInstruments.Core.ArgumentValidation;
using RapidField.SolidInstruments.Core.Concurrency;
using System;
using System.Diagnostics;
using System.Threading;
namespace RapidField.SolidInstruments.Core
{
/// <summary>
/// Represents a lazily-configurable utility with disposable resources and exposes a lazily-loaded concurrency control
/// mechanism.
/// </summary>
/// <typeparam name="TConfiguration">
/// The type of the configuration information for the instrument.
/// </typeparam>
public abstract class ConfigurableInstrument<TConfiguration> : Instrument
where TConfiguration : IInstrumentConfiguration, new()
{
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurableInstrument{TConfiguration}" /> class.
/// </summary>
protected ConfigurableInstrument()
: this(DefaultConfiguration)
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurableInstrument{TConfiguration}" /> class.
/// </summary>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="applicationConfiguration" /> is <see langword="null" />.
/// </exception>
protected ConfigurableInstrument(IConfiguration applicationConfiguration)
: base()
{
ApplicationConfiguration = applicationConfiguration.RejectIf().IsNull(nameof(applicationConfiguration)).TargetArgument;
LazyConfiguration = new Lazy<TConfiguration>(Configure, LazyThreadSafetyMode.PublicationOnly);
}
/// <summary>
/// Initializes a concurrency control mechanism that is used to manage state for the current <see cref="Instrument" />.
/// </summary>
/// <returns>
/// A new instance of the <see cref="SemaphoreSlim" /> class.
/// </returns>
/// <exception cref="ObjectConfigurationException">
/// An exception was raised during configuration of the instrument.
/// </exception>
[DebuggerHidden]
internal sealed override IConcurrencyControl InitializeStateControl()
{
StateControlMode = Configuration.StateControlMode;
StateControlTimeoutThreshold = Configuration.StateControlTimeoutThreshold;
return base.InitializeStateControl();
}
/// <summary>
/// Configures the current <see cref="ConfigurableInstrument{TConfiguration}" />.
/// </summary>
/// <param name="configuration">
/// Configuration information for the current <see cref="ConfigurableInstrument{TConfiguration}" />.
/// </param>
protected abstract void Configure(TConfiguration configuration);
/// <summary>
/// Releases all resources consumed by the current <see cref="ConfigurableInstrument{TConfiguration}" />.
/// </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>
/// Initializes a default <see cref="IConfiguration" /> instance.
/// </summary>
/// <returns>
/// A default <see cref="IConfiguration" /> instance.
/// </returns>
[DebuggerHidden]
private static IConfiguration InitializeDefaultConfiguration() => new ConfigurationBuilder().Build();
/// <summary>
/// Configures the current <see cref="ConfigurableInstrument{TConfiguration}" />.
/// </summary>
/// <returns>
/// Configuration information for the current <see cref="ConfigurableInstrument{TConfiguration}" />.
/// </returns>
/// <exception cref="ObjectConfigurationException">
/// An exception was raised during configuration of the instrument.
/// </exception>
[DebuggerHidden]
private TConfiguration Configure()
{
try
{
var configuration = new TConfiguration()
{
Application = ApplicationConfiguration
};
Configure(configuration);
return configuration;
}
catch (Exception exception)
{
throw new ObjectConfigurationException(GetType(), exception);
}
}
/// <summary>
/// Gets a default <see cref="IConfiguration" /> instance.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal static IConfiguration DefaultConfiguration => LazyDefaultConfiguration.Value;
/// <summary>
/// Gets configuration information for the current <see cref="ConfigurableInstrument{TConfiguration}" />.
/// </summary>
/// <exception cref="ObjectConfigurationException">
/// An exception was raised during configuration of the instrument.
/// </exception>
protected TConfiguration Configuration => LazyConfiguration.Value;
/// <summary>
/// Represents a lazily-initialized default <see cref="IConfiguration" /> instance.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static readonly Lazy<IConfiguration> LazyDefaultConfiguration = new Lazy<IConfiguration>(InitializeDefaultConfiguration, LazyThreadSafetyMode.PublicationOnly);
/// <summary>
/// Represents configuration information for the application.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IConfiguration ApplicationConfiguration;
/// <summary>
/// Represents lazily-initialized configuration information for the current
/// <see cref="ConfigurableInstrument{TConfiguration}" />.
/// </summary>
/// <exception cref="ObjectConfigurationException">
/// An exception was raised during configuration of the instrument.
/// </exception>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Lazy<TConfiguration> LazyConfiguration;
}
}