-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjectFactoryProductionFunction.cs
More file actions
120 lines (111 loc) · 4.2 KB
/
ObjectFactoryProductionFunction.cs
File metadata and controls
120 lines (111 loc) · 4.2 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
// =================================================================================================================================
// 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.Diagnostics;
namespace RapidField.SolidInstruments.ObjectComposition
{
/// <summary>
/// Represents a function that produces an object of a specified type.
/// </summary>
/// <typeparam name="TProduct">
/// The type of the object produced by the function.
/// </typeparam>
internal sealed class ObjectFactoryProductionFunction<TProduct> : ObjectFactoryProductionFunction
where TProduct : class
{
/// <summary>
/// Initializes a new instance of the <see cref="ObjectFactoryProductionFunction{TProduct}" /> class.
/// </summary>
/// <param name="function">
/// A function that produces an output.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="function" /> is <see langword="null" />.
/// </exception>
[DebuggerHidden]
public ObjectFactoryProductionFunction(Func<TProduct> function)
: base()
{
ProductType = typeof(TProduct);
Function = function.RejectIf().IsNull(nameof(function));
}
/// <summary>
/// Invokes the function and returns an output.
/// </summary>
/// <returns>
/// The function output.
/// </returns>
/// <exception cref="ObjectProductionException">
/// An exception was raised during object production.
/// </exception>
[DebuggerHidden]
public sealed override Object Invoke()
{
try
{
return Function();
}
catch (Exception exception)
{
throw new ObjectProductionException(ProductType, exception);
}
}
/// <summary>
/// Gets the type of the object produced by the function.
/// </summary>
public sealed override Type ProductType
{
get;
}
/// <summary>
/// Represents a function that produces an output.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Func<TProduct> Function;
}
/// <summary>
/// Represents a function that produces an object of a specified type.
/// </summary>
/// <remarks>
/// <see cref="ObjectFactoryProductionFunction" /> is the default implementation of
/// <see cref="IObjectFactoryProductionFunction" />.
/// </remarks>
internal abstract class ObjectFactoryProductionFunction : IObjectFactoryProductionFunction
{
/// <summary>
/// Initializes a new instance of the <see cref="ObjectFactoryProductionFunction" /> class.
/// </summary>
[DebuggerHidden]
protected ObjectFactoryProductionFunction()
{
return;
}
/// <summary>
/// Invokes the function and returns an output object.
/// </summary>
/// <returns>
/// The function output.
/// </returns>
/// <exception cref="ObjectProductionException">
/// An exception was raised during object production.
/// </exception>
public abstract Object Invoke();
/// <summary>
/// Converts the value of the current <see cref="ObjectFactoryProductionFunction" /> to its equivalent string
/// representation.
/// </summary>
/// <returns>
/// A string representation of the current <see cref="ObjectFactoryProductionFunction" />.
/// </returns>
public override String ToString() => $"{{ \"{nameof(ProductType)}\": \"{ProductType.FullName}\" }}";
/// <summary>
/// Gets the type of the object produced by the function.
/// </summary>
public abstract Type ProductType
{
get;
}
}
}