-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandMediator.cs
More file actions
147 lines (136 loc) · 6.3 KB
/
CommandMediator.cs
File metadata and controls
147 lines (136 loc) · 6.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
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
// =================================================================================================================================
// 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 RapidField.SolidInstruments.InversionOfControl;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace RapidField.SolidInstruments.Command
{
/// <summary>
/// Serves as a dependency resolver and processing intermediary for commands.
/// </summary>
/// <remarks>
/// <see cref="CommandMediator" /> is the default implementation of <see cref="ICommandMediator" />.
/// </remarks>
public sealed class CommandMediator : Instrument, ICommandMediator
{
/// <summary>
/// Initializes a new instance of the <see cref="CommandMediator" /> class.
/// </summary>
/// <param name="scope">
/// The object initialization and disposal scope for the mediator.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="scope" /> is <see langword="null" />.
/// </exception>
public CommandMediator(IDependencyScope scope)
: base(ConcurrencyControlMode.ProcessorCountSemaphore)
{
Scope = scope.RejectIf().IsNull(nameof(scope)).TargetArgument;
}
/// <summary>
/// Processes the specified <see cref="ICommand{TResult}" />.
/// </summary>
/// <typeparam name="TResult">
/// The type of the result that is produced by processing the command.
/// </typeparam>
/// <param name="command">
/// The command to process.
/// </param>
/// <returns>
/// The result that is produced by processing the command.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="command" /> is <see langword="null" />.
/// </exception>
/// <exception cref="CommandHandlingException">
/// An exception was raised while processing the command.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
public TResult Process<TResult>(ICommand<TResult> command)
{
var commandType = command.RejectIf().IsNull(nameof(command)).TargetArgument.GetType();
try
{
var registeredHandlerType = CommandHandlerRegisteredType.MakeGenericType(commandType);
var implementedHandlerType = CommandHandlerImplementedType.MakeGenericType(commandType, typeof(TResult));
var processMethod = implementedHandlerType.GetMethod(CommandHandlerProcessMethodName);
var processMethodArguments = new Object[1] { command };
using (var controlToken = StateControl.Enter())
{
RejectIfDisposed();
var commandHandler = Scope.Resolve(registeredHandlerType);
var result = processMethod.Invoke(commandHandler, processMethodArguments);
return (TResult)result;
}
}
catch (CommandHandlingException)
{
throw;
}
catch (ObjectDisposedException)
{
throw;
}
catch (Exception exception)
{
throw new CommandHandlingException(commandType, exception);
}
}
/// <summary>
/// Asynchronously processes the specified <see cref="ICommand{TResult}" />.
/// </summary>
/// <typeparam name="TResult">
/// The type of the result that is produced by processing the command.
/// </typeparam>
/// <param name="command">
/// The command to process.
/// </param>
/// <returns>
/// A task representing the asynchronous operation and containing the result that is produced by processing the command.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="command" /> is <see langword="null" />.
/// </exception>
/// <exception cref="CommandHandlingException">
/// An exception was raised while processing the command.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
public Task<TResult> ProcessAsync<TResult>(ICommand<TResult> command) => Task.FromResult(Process(command));
/// <summary>
/// Releases all resources consumed by the current <see cref="CommandMediator" />.
/// </summary>
/// <param name="disposing">
/// A value indicating whether or not managed resources should be released.
/// </param>
protected sealed override void Dispose(Boolean disposing) => base.Dispose(disposing);
/// <summary>
/// Represents the name of the <see cref="ICommandHandler{TCommand, TResult}.Process(TCommand)" /> method.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String CommandHandlerProcessMethodName = "Process";
/// <summary>
/// Represents the generic <see cref="ICommandHandler{TCommand, TResult}" /> type.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static readonly Type CommandHandlerImplementedType = typeof(ICommandHandler<,>);
/// <summary>
/// Represents the generic <see cref="ICommandHandler{TCommand}" /> type.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static readonly Type CommandHandlerRegisteredType = typeof(ICommandHandler<>);
/// <summary>
/// Represents the object initialization and disposal scope for the current <see cref="CommandMediator" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IDependencyScope Scope;
}
}