This repository has been archived by the owner on Feb 18, 2018. It is now read-only.
forked from spontoreau/Bartender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dispatcher.cs
143 lines (123 loc) · 5.04 KB
/
Dispatcher.cs
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
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Bartender
{
/// <summary>
/// Dispatcher.
/// </summary>
public class Dispatcher : IDispatcher,
IAsyncDispatcher,
ICancellableAsyncDispatcher
{
/// <summary>
/// Initializes a new instance of the Dispatcher class.
/// </summary>
/// <param name="container">Dependency container.</param>
public Dispatcher(IDependencyContainer container)
{
Container = container;
}
/// <summary>
/// Dependency container.
/// </summary>
protected IDependencyContainer Container { get; }
/// <summary>
/// Dispatch a message
/// </summary>
/// <param name="message">Message to dispatch</param>
/// <returns>Result</returns>
TResult IDispatcher.Dispatch<TMessage, TResult>(TMessage message)
=>
Validate(message)
.GetHandlers<IHandler<TMessage, TResult>>()
.Single()
.Handle(message);
/// <summary>
/// Dispatch the specified message.
/// </summary>
/// <param name="message">Message.</param>
void IDispatcher.Dispatch<TMessage>(TMessage message)
{
var handlers = Validate(message).GetHandlers<IHandler<TMessage>>();
foreach(var h in handlers)
h.Handle(message);
}
/// <summary>
/// Dispatch a message asynchronously.
/// </summary>
/// <param name="message">Message to dispatch</param>
/// <returns>Result</returns>
async Task<TResult> IAsyncDispatcher.DispatchAsync<TMessage, TResult>(TMessage message)
=>
await Validate(message)
.GetHandlers<IAsyncHandler<TMessage, TResult>>()
.Single()
.HandleAsync(message);
/// <summary>
/// Dispatch a message asynchronously.
/// </summary>
/// <param name="message">Message to dispatch</param>
async Task IAsyncDispatcher.DispatchAsync<TMessage>(TMessage message)
{
var handlers = Validate(message).GetHandlers<IAsyncHandler<TMessage>>();
foreach(var h in handlers)
await h.HandleAsync(message);
}
/// <summary>
/// Dispatch a message asynchronously.
/// </summary>
/// <param name="message">Message to dispatch</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Result</returns>
async Task<TResult> ICancellableAsyncDispatcher.DispatchAsync<TMessage, TResult>(TMessage message, CancellationToken cancellationToken)
=>
await Validate(message)
.GetHandlers<ICancellableAsyncHandler<TMessage, TResult>>()
.Single()
.HandleAsync(message, cancellationToken);
/// <summary>
/// Dispatch a message asynchronously.
/// </summary>
/// <param name="message">Message to dispatch</param>
/// <param name="cancellationToken">Cancellation token</param>
async Task ICancellableAsyncDispatcher.DispatchAsync<TMessage>(TMessage message, CancellationToken cancellationToken)
{
var handlers = Validate(message).GetHandlers<ICancellableAsyncHandler<TMessage>>();
foreach(var h in handlers)
await h.HandleAsync(message, cancellationToken);
}
/// <summary>
/// Get handler
/// </summary>
/// <returns>Enumerable of handlers</returns>
protected IEnumerable<THandler> GetHandlers<THandler>()
{
var handlers = Container.GetAllInstances<THandler>();
var messageType = typeof(THandler).GenericTypeArguments.First();
if(!handlers.Any()) throw new DispatcherException($"No handler for '{messageType.FullName}'.");
if(!IsPublication(messageType))
if(handlers.Count() > 1) throw new DispatcherException($"Multiple handler for '{messageType.FullName}'.");
return handlers;
}
/// <summary>
/// Message validation
/// </summary>
protected Dispatcher Validate<TMessage>(TMessage message)
where TMessage : IMessage
{
var validators = Container.GetAllInstances<IMessageValidator<TMessage>>();
foreach (var validator in validators)
validator.Validate(message);
return this;
}
/// <summary>
/// True if a type is a publication, otherwise false
/// </summary>
/// <returns>True if a type is a publication, otherwise false</returns>
protected bool IsPublication(Type type) => type.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IPublication));
}
}