Repository navigation
Expand file tree
/
Copy pathQuick.Core.Mediator.pas
More file actions
309 lines (256 loc) · 11.5 KB
/
Copy pathQuick.Core.Mediator.pas
File metadata and controls
309 lines (256 loc) · 11.5 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
{ ***************************************************************************
Copyright (c) 2016-2026 Kike Perez
Unit : Quick.Core.Mediator
Description : TMediator — request/notification dispatcher with pipeline support
Author : Kike Perez
Version : 1.0
Created : 29/04/2026
Modified : 08/05/2026
This file is part of QuickCore: https://github.com/exilon/QuickCore
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.Core.Mediator;
{$i QuickCore.inc}
interface
uses
System.Classes,
System.Rtti,
System.SysUtils,
System.Generics.Collections,
Quick.Core.Mediator.Abstractions;
type
// ── Factory delegates ───────────────────────────────────────────────────────
/// <summary>Factory that produces a request handler on demand.</summary>
THandlerFactory = reference to function: IRequestHandlerBase;
/// <summary>Factory that produces a notification handler on demand.</summary>
TNotifHandlerFactory = reference to function: INotificationHandlerBase;
/// <summary>Factory that produces a pipeline behavior on demand.</summary>
TPipelineFactory = reference to function: IPipelineBehaviorBase;
// ── Registry ────────────────────────────────────────────────────────────────
/// <summary>
/// Central registry that maps request/notification class names to their
/// handler factories. Populated at startup via TMediatorBuilder and shared
/// with TMediator as a singleton.
/// </summary>
TMediatorRegistry = class
private
fRequestHandlers : TDictionary<string, THandlerFactory>;
fNotifHandlers : TObjectDictionary<string, TList<TNotifHandlerFactory>>;
fPipelineBehaviors : TList<TPipelineFactory>;
public
constructor Create;
destructor Destroy; override;
procedure AddRequestHandler(const aRequestClassName: string; aFactory: THandlerFactory);
procedure AddNotifHandler(const aNotifClassName: string; aFactory: TNotifHandlerFactory);
procedure AddPipelineBehavior(aFactory: TPipelineFactory);
function HasRequestHandler(const aRequestClassName: string): Boolean;
function GetRequestHandler(const aRequestClassName: string): THandlerFactory;
function GetNotifHandlers(const aNotifClassName: string): TArray<TNotifHandlerFactory>;
function GetPipelineBehaviors: TArray<TPipelineFactory>;
end;
// ── Mediator ─────────────────────────────────────────────────────────────────
/// <summary>
/// Dispatches requests to their registered handler and publishes notifications
/// to all registered handlers. Pipeline behaviors wrap the handler chain.
///
/// Register via Quick.Core.Extensions.Mediator:
/// services.Extension<TMediatorServiceExtension>
/// .AddMediatR
/// .AddRequestHandler<TMyQuery, TMyResponse, TMyQueryHandler>
/// .AddNotificationHandler<TMyEvent, TMyEventHandler>;
///
/// Then inject:
/// constructor TMyController.Create(aMediator: IMediator);
/// result := aMediator.Send(TMyQuery.Create(id)).AsType<TMyResponse>;
///
/// Or for type-safe dispatch, inject TMediator directly:
/// result := aMediator.SendRequest<TMyResponse>(TMyQuery.Create(id));
/// </summary>
TMediator = class(TInterfacedObject, IMediator)
private
fRegistry: TMediatorRegistry;
function BuildChain(aHandler: IRequestHandlerBase;
const aBehaviors: TArray<TPipelineFactory>): TPipelineDelegate;
public
constructor Create(aRegistry: TMediatorRegistry);
// ── IMediator ──────────────────────────────────────────────────────────
/// <summary>Send a request to its single registered handler.
/// The pipeline behaviors (if any) are applied in registration order.
/// Raises EMediatorError if no handler is registered for the request type.</summary>
function Send(const aRequest: TObject): TValue;
/// <summary>Publish a notification to all registered handlers.
/// Handlers are called in registration order. Errors in one handler
/// do NOT prevent subsequent handlers from running.</summary>
procedure Publish(const aNotification: TObject);
// ── Generic convenience (type-safe) ────────────────────────────────────
/// <summary>Type-safe Send. Returns TResponse directly without TValue unwrapping.
/// Requires injecting TMediator (not IMediator) — use when you control the
/// injection point and prefer compile-time type safety.</summary>
function SendRequest<TResponse>(const aRequest: TObject): TResponse;
end;
// ── Internal pipeline chain builder (module-scope, not in interface) ────────
function BuildChainLink(aBehavior: IPipelineBehaviorBase;
aNext: TPipelineDelegate): TPipelineDelegate;
implementation
// ── TMediatorRegistry ────────────────────────────────────────────────────────
constructor TMediatorRegistry.Create;
begin
inherited Create;
fRequestHandlers := TDictionary<string, THandlerFactory>.Create;
fNotifHandlers := TObjectDictionary<string, TList<TNotifHandlerFactory>>.Create([doOwnsValues]);
fPipelineBehaviors := TList<TPipelineFactory>.Create;
end;
destructor TMediatorRegistry.Destroy;
begin
fRequestHandlers.Free;
fNotifHandlers.Free;
fPipelineBehaviors.Free;
inherited;
end;
procedure TMediatorRegistry.AddRequestHandler(const aRequestClassName: string;
aFactory: THandlerFactory);
begin
fRequestHandlers.AddOrSetValue(aRequestClassName, aFactory);
end;
procedure TMediatorRegistry.AddNotifHandler(const aNotifClassName: string;
aFactory: TNotifHandlerFactory);
var
list: TList<TNotifHandlerFactory>;
begin
if not fNotifHandlers.TryGetValue(aNotifClassName, list) then
begin
list := TList<TNotifHandlerFactory>.Create;
fNotifHandlers.Add(aNotifClassName, list);
end;
list.Add(aFactory);
end;
procedure TMediatorRegistry.AddPipelineBehavior(aFactory: TPipelineFactory);
begin
fPipelineBehaviors.Add(aFactory);
end;
function TMediatorRegistry.HasRequestHandler(const aRequestClassName: string): Boolean;
begin
Result := fRequestHandlers.ContainsKey(aRequestClassName);
end;
function TMediatorRegistry.GetRequestHandler(const aRequestClassName: string): THandlerFactory;
begin
if not fRequestHandlers.TryGetValue(aRequestClassName, Result) then
raise EMediatorError.CreateFmt(
'Mediator: no handler registered for request type "%s". ' +
'Did you call AddRequestHandler<%s,...>() in AddMediatR?',
[aRequestClassName, aRequestClassName]);
end;
function TMediatorRegistry.GetNotifHandlers(
const aNotifClassName: string): TArray<TNotifHandlerFactory>;
var
list: TList<TNotifHandlerFactory>;
begin
if fNotifHandlers.TryGetValue(aNotifClassName, list) then
Result := list.ToArray
else
SetLength(Result, 0);
end;
function TMediatorRegistry.GetPipelineBehaviors: TArray<TPipelineFactory>;
begin
Result := fPipelineBehaviors.ToArray;
end;
// ── Pipeline chain builder ────────────────────────────────────────────────────
function BuildChainLink(aBehavior: IPipelineBehaviorBase;
aNext: TPipelineDelegate): TPipelineDelegate;
begin
// Each captured aBehavior + aNext pair is independent — no loop-variable aliasing.
Result := function(const req: TObject): TValue
begin
Result := aBehavior.HandleObj(req, aNext);
end;
end;
// ── TMediator ─────────────────────────────────────────────────────────────────
constructor TMediator.Create(aRegistry: TMediatorRegistry);
begin
inherited Create;
fRegistry := aRegistry;
end;
function TMediator.BuildChain(aHandler: IRequestHandlerBase;
const aBehaviors: TArray<TPipelineFactory>): TPipelineDelegate;
var
i : Integer;
innerChain: TPipelineDelegate;
begin
// Innermost link: the actual handler
innerChain := function(const req: TObject): TValue
begin
Result := aHandler.HandleObj(req);
end;
// Wrap behaviors from innermost to outermost (reverse order ensures
// first-registered behavior is the outermost / first to execute)
for i := High(aBehaviors) downto 0 do
innerChain := BuildChainLink(aBehaviors[i](), innerChain);
Result := innerChain;
end;
function TMediator.Send(const aRequest: TObject): TValue;
var
handlerFactory : THandlerFactory;
handler : IRequestHandlerBase;
behaviors : TArray<TPipelineFactory>;
chain : TPipelineDelegate;
begin
if aRequest = nil then
raise EMediatorError.Create('Mediator.Send: request cannot be nil');
handlerFactory := fRegistry.GetRequestHandler(aRequest.ClassName);
handler := handlerFactory();
behaviors := fRegistry.GetPipelineBehaviors;
chain := BuildChain(handler, behaviors);
Result := chain(aRequest);
end;
procedure TMediator.Publish(const aNotification: TObject);
var
factories : TArray<TNotifHandlerFactory>;
factory : TNotifHandlerFactory;
handler : INotificationHandlerBase;
errors : TStringList;
begin
if aNotification = nil then
raise EMediatorError.Create('Mediator.Publish: notification cannot be nil');
factories := fRegistry.GetNotifHandlers(aNotification.ClassName);
if Length(factories) = 0 then Exit; // no handlers registered is valid (no-op)
errors := TStringList.Create;
try
for factory in factories do
begin
try
handler := factory();
handler.HandleObj(aNotification);
except
on E: Exception do
errors.Add(Format('[%s] %s', [aNotification.ClassName, E.Message]));
end;
end;
// Surface any errors after all handlers have run
if errors.Count > 0 then
raise EMediatorError.CreateFmt(
'Mediator.Publish: %d handler(s) failed for "%s":%s%s',
[errors.Count, aNotification.ClassName, sLineBreak, errors.Text.TrimRight]);
finally
errors.Free;
end;
end;
function TMediator.SendRequest<TResponse>(const aRequest: TObject): TResponse;
var
v: TValue;
begin
v := Send(aRequest);
if v.IsEmpty then
Result := Default(TResponse)
else
Result := v.AsType<TResponse>;
end;
end.