/
Mediator.cs
157 lines (129 loc) · 5.24 KB
/
Mediator.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
namespace ShortBus
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
public interface IMediator
{
Response<TResponseData> Request<TResponseData>(IRequest<TResponseData> request);
Task<Response<TResponseData>> RequestAsync<TResponseData>(IAsyncRequest<TResponseData> query);
Response Notify<TNotification>(TNotification notification);
Task<Response> NotifyAsync<TNotification>(TNotification notification);
}
public class Mediator : IMediator
{
private readonly IDependencyResolver _dependencyResolver;
public Mediator(IDependencyResolver dependencyResolver)
{
_dependencyResolver = dependencyResolver;
}
public virtual Response<TResponseData> Request<TResponseData>(IRequest<TResponseData> request)
{
var response = new Response<TResponseData>();
try
{
var plan = new MediatorPlan<TResponseData>(typeof (IRequestHandler<,>), "Handle", request.GetType(),
_dependencyResolver);
response.Data = plan.Invoke(request);
}
catch (Exception e)
{
response.Exception = e;
}
return response;
}
public async Task<Response<TResponseData>> RequestAsync<TResponseData>(IAsyncRequest<TResponseData> query)
{
var response = new Response<TResponseData>();
try
{
var plan = new MediatorPlan<TResponseData>(typeof (IAsyncRequestHandler<,>), "HandleAsync",
query.GetType(), _dependencyResolver);
response.Data = await plan.InvokeAsync(query);
}
catch (Exception e)
{
response.Exception = e;
}
return response;
}
public Response Notify<TNotification>(TNotification notification)
{
var handlers = _dependencyResolver.GetInstances<INotificationHandler<TNotification>>();
var response = new Response();
List<Exception> exceptions = null;
foreach (var handler in handlers)
try
{
handler.Handle(notification);
}
catch (Exception e)
{
(exceptions ?? (exceptions = new List<Exception>())).Add(e);
}
if (exceptions != null)
response.Exception = new AggregateException(exceptions);
return response;
}
public async Task<Response> NotifyAsync<TNotification>(TNotification notification)
{
var handlers = _dependencyResolver.GetInstances<IAsyncNotificationHandler<TNotification>>();
return await Task
.WhenAll(handlers.Select(x => notifyAsync(x, notification)))
.ContinueWith(task =>
{
var exceptions = task.Result.Where(exception => exception != null).ToArray();
var response = new Response();
if (exceptions.Any())
{
response.Exception = new AggregateException(exceptions);
}
return response;
});
}
private static async Task<Exception> notifyAsync<TNotification>(
IAsyncNotificationHandler<TNotification> asyncCommandHandler, TNotification message)
{
try
{
await asyncCommandHandler.HandleAsync(message);
}
catch (Exception e)
{
return e;
}
return null;
}
private class MediatorPlan<TResult>
{
private readonly MethodInfo HandleMethod;
private readonly Func<object> HandlerInstanceBuilder;
public MediatorPlan(Type handlerTypeTemplate, string handlerMethodName, Type messageType,
IDependencyResolver dependencyResolver)
{
var handlerType = handlerTypeTemplate.MakeGenericType(messageType, typeof (TResult));
HandleMethod = GetHandlerMethod(handlerType, handlerMethodName, messageType);
HandlerInstanceBuilder = () => dependencyResolver.GetInstance(handlerType);
}
private MethodInfo GetHandlerMethod(Type handlerType, string handlerMethodName, Type messageType)
{
return handlerType
.GetMethod(handlerMethodName,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
null, CallingConventions.HasThis,
new[] {messageType},
null);
}
public TResult Invoke(object message)
{
return (TResult) HandleMethod.Invoke(HandlerInstanceBuilder(), new[] {message});
}
public async Task<TResult> InvokeAsync(object message)
{
return await (Task<TResult>) HandleMethod.Invoke(HandlerInstanceBuilder(), new[] {message});
}
}
}
}