-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjectBuilder.cs
More file actions
78 lines (73 loc) · 3 KB
/
ObjectBuilder.cs
File metadata and controls
78 lines (73 loc) · 3 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
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using RapidField.SolidInstruments.Core.Concurrency;
using System;
namespace RapidField.SolidInstruments.Core
{
/// <summary>
/// Represents an object that configures and produces new <typeparamref name="TResult" /> instances.
/// </summary>
/// <remarks>
/// <see cref="ObjectBuilder{TResult}" /> is the default implementation of <see cref="IObjectBuilder{TResult}" />.
/// </remarks>
/// <typeparam name="TResult">
/// The output type that results from the invocation of <see cref="ObjectBuilder{TResult}.ToResult()" />.
/// </typeparam>
public abstract class ObjectBuilder<TResult> : Instrument, IObjectBuilder<TResult>
where TResult : class
{
/// <summary>
/// Initializes a new instance of the <see cref="ObjectBuilder{TResult}" /> class.
/// </summary>
protected ObjectBuilder()
: base()
{
return;
}
/// <summary>
/// Produces the configured <typeparamref name="TResult" /> instance.
/// </summary>
/// <returns>
/// The configured <typeparamref name="TResult" /> instance.
/// </returns>
/// <exception cref="ObjectBuilderException">
/// An exception was raised during finalization of the builder.
/// </exception>
public TResult ToResult()
{
using (var controlToken = StateControl.Enter())
{
try
{
return ToResult(controlToken);
}
catch (ObjectBuilderException)
{
throw;
}
catch (Exception exception)
{
throw new ObjectBuilderException(GetType(), exception);
}
}
}
/// <summary>
/// Releases all resources consumed by the current <see cref="ObjectBuilder{TResult}" />.
/// </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 <typeparamref name="TResult" /> instance.
/// </summary>
/// <param name="controlToken">
/// A token that represents and manages contextual thread safety.
/// </param>
/// <returns>
/// The configured <typeparamref name="TResult" /> instance.
/// </returns>
protected abstract TResult ToResult(IConcurrencyControlToken controlToken);
}
}