Skip to content

Commit 9878f97

Browse files
committed
- Checkpoint: Split Aggregate and Projection dispatchers
1 parent 88a1184 commit 9878f97

File tree

4 files changed

+164
-111
lines changed

4 files changed

+164
-111
lines changed

src/SourceFlow/ClassDiagram.cd

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
<FileName>Saga\Saga.cs</FileName>
1818
</TypeIdentifier>
1919
<ShowAsAssociation>
20-
<Field Name="eventQueue" />
2120
<Field Name="commandPublisher" />
21+
<Field Name="eventQueue" />
2222
<Field Name="repository" />
2323
</ShowAsAssociation>
2424
<Lollipop Position="0.2" />
@@ -53,13 +53,13 @@
5353
<Lollipop Position="0.2" />
5454
</Class>
5555
<Class Name="SourceFlow.Aggregate.Aggregate&lt;TAggregate&gt;">
56-
<Position X="16.5" Y="3.5" Width="1.75" />
56+
<Position X="17.75" Y="3.5" Width="1.75" />
5757
<AssociationLine Name="commandPublisher" Type="SourceFlow.Messaging.Bus.ICommandPublisher" ManuallyRouted="true" FixedFromPoint="true" FixedToPoint="true">
5858
<Path>
59-
<Point X="18.25" Y="3.938" />
60-
<Point X="18.631" Y="3.938" />
61-
<Point X="18.631" Y="0.705" />
62-
<Point X="3.562" Y="0.705" />
59+
<Point X="19.5" Y="3.938" />
60+
<Point X="20.031" Y="3.938" />
61+
<Point X="20.031" Y="1.125" />
62+
<Point X="3.562" Y="1.125" />
6363
<Point X="3.562" Y="1.5" />
6464
</Path>
6565
<MemberNameLabel ManuallyPlaced="true">
@@ -68,9 +68,9 @@
6868
</AssociationLine>
6969
<AssociationLine Name="commandReplayer" Type="SourceFlow.Messaging.Bus.ICommandReplayer" ManuallyRouted="true" FixedFromPoint="true" FixedToPoint="true">
7070
<Path>
71-
<Point X="18.25" Y="4.312" />
72-
<Point X="19.066" Y="4.312" />
73-
<Point X="19.066" Y="0.314" />
71+
<Point X="19.5" Y="4.312" />
72+
<Point X="20.531" Y="4.312" />
73+
<Point X="20.531" Y="0.314" />
7474
<Point X="1.125" Y="0.314" />
7575
<Point X="1.125" Y="1.5" />
7676
</Path>
@@ -176,20 +176,20 @@
176176
<Lollipop Position="0.2" />
177177
</Class>
178178
<Class Name="SourceFlow.Impl.AggregateFactory">
179-
<Position X="16.5" Y="7" Width="2" />
179+
<Position X="17.75" Y="7" Width="2" />
180180
<TypeIdentifier>
181181
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAABAEAAAAAAAAAAAAA=</HashCode>
182182
<FileName>Impl\AggregateFactory.cs</FileName>
183183
</TypeIdentifier>
184184
<Lollipop Position="0.2" />
185185
</Class>
186186
<Class Name="SourceFlow.Services.Service">
187-
<Position X="19" Y="6.25" Width="1.75" />
187+
<Position X="20.25" Y="6.25" Width="1.75" />
188188
<AssociationLine Name="aggregateFactory" Type="SourceFlow.Aggregate.IAggregateFactory" FixedFromPoint="true" FixedToPoint="true">
189189
<Path>
190-
<Point X="20.25" Y="6.25" />
191-
<Point X="20.25" Y="5.746" />
192-
<Point X="18.25" Y="5.746" />
190+
<Point X="21.5" Y="6.25" />
191+
<Point X="21.5" Y="5.746" />
192+
<Point X="19.5" Y="5.746" />
193193
</Path>
194194
</AssociationLine>
195195
<TypeIdentifier>
@@ -201,55 +201,60 @@
201201
</ShowAsAssociation>
202202
<Lollipop Position="0.2" />
203203
</Class>
204-
<Class Name="SourceFlow.Impl.EventDispatcher">
205-
<Position X="12.25" Y="3.5" Width="1.5" />
206-
<AssociationLine Name="projections" Type="SourceFlow.ViewModel.IProjection" FixedFromPoint="true" FixedToPoint="true">
204+
<Class Name="SourceFlow.Impl.CommandDispatcher">
205+
<Position X="3" Y="7.5" Width="2" />
206+
<AssociationLine Name="sagas" Type="SourceFlow.Saga.ISaga" ManuallyRouted="true" FixedFromPoint="true" FixedToPoint="true">
207207
<Path>
208-
<Point X="13" Y="5.632" />
209-
<Point X="13" Y="5.918" />
210-
<Point X="15.188" Y="5.918" />
211-
<Point X="15.188" Y="5.618" />
208+
<Point X="5" Y="8.245" />
209+
<Point X="5.375" Y="8.245" />
210+
<Point X="5.375" Y="4.552" />
211+
<Point X="6" Y="4.552" />
212212
</Path>
213-
<MemberNameLabel ManuallyPlaced="true">
214-
<Position X="0.315" Y="0.132" />
215-
</MemberNameLabel>
216213
</AssociationLine>
217-
<AssociationLine Name="aggregates" Type="SourceFlow.Aggregate.IAggregate" FixedFromPoint="true" FixedToPoint="true">
214+
<TypeIdentifier>
215+
<HashCode>AAgAAAAAAAAAAEACAEAAAAAAAAABAAAAAAAAAAAAAAA=</HashCode>
216+
<FileName>Impl\CommandDispatcher.cs</FileName>
217+
</TypeIdentifier>
218+
<ShowAsCollectionAssociation>
219+
<Field Name="sagas" />
220+
</ShowAsCollectionAssociation>
221+
<Lollipop Position="0.2" />
222+
</Class>
223+
<Class Name="SourceFlow.Impl.ProjectionDispatcher">
224+
<Position X="12.5" Y="6.25" Width="1.75" />
225+
<AssociationLine Name="projections" Type="SourceFlow.ViewModel.IProjection" FixedFromPoint="true" FixedToPoint="true">
218226
<Path>
219-
<Point X="13.75" Y="4.375" />
220-
<Point X="15.438" Y="4.375" />
221-
<Point X="15.438" Y="4.062" />
227+
<Point X="14.25" Y="6.688" />
228+
<Point X="14.625" Y="6.688" />
229+
<Point X="14.625" Y="5.938" />
230+
<Point X="15.5" Y="5.938" />
222231
</Path>
223232
<MemberNameLabel ManuallyPlaced="true">
224-
<Position X="0.584" Y="0.11" />
233+
<Position X="0.138" Y="0.157" />
225234
</MemberNameLabel>
226235
</AssociationLine>
227236
<TypeIdentifier>
228-
<HashCode>AAgAAAgAAAAAAAgCAAAAAAAAAACAAAAAACAAAAAAAAA=</HashCode>
229-
<FileName>Impl\EventDispatcher.cs</FileName>
237+
<HashCode>AAgAAAAAAAAAAAACAAAAAAAAAAAAAAAAACAAAAAAAAA=</HashCode>
238+
<FileName>Impl\ProjectionDispatcher.cs</FileName>
230239
</TypeIdentifier>
231240
<ShowAsCollectionAssociation>
232241
<Field Name="projections" />
233-
<Field Name="aggregates" />
234242
</ShowAsCollectionAssociation>
235-
<Lollipop Position="0.1" />
243+
<Lollipop Position="0.2" />
236244
</Class>
237-
<Class Name="SourceFlow.Impl.CommandDispatcher">
238-
<Position X="3" Y="7.5" Width="2" />
239-
<AssociationLine Name="sagas" Type="SourceFlow.Saga.ISaga" ManuallyRouted="true" FixedFromPoint="true" FixedToPoint="true">
240-
<Path>
241-
<Point X="5" Y="8.245" />
242-
<Point X="5.375" Y="8.245" />
243-
<Point X="5.375" Y="4.552" />
244-
<Point X="6" Y="4.552" />
245-
</Path>
245+
<Class Name="SourceFlow.Impl.AggregateDispatcher">
246+
<Position X="12.5" Y="3.75" Width="1.75" />
247+
<AssociationLine Name="aggregates" Type="SourceFlow.Aggregate.IAggregate">
248+
<MemberNameLabel ManuallyPlaced="true">
249+
<Position X="0.235" Y="0.06" />
250+
</MemberNameLabel>
246251
</AssociationLine>
247252
<TypeIdentifier>
248-
<HashCode>AAgAAAAAAAAAAEACAEAAAAAAAAABAAAAAAAAAAAAAAA=</HashCode>
249-
<FileName>Impl\CommandDispatcher.cs</FileName>
253+
<HashCode>AAgAAAgAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
254+
<FileName>Impl\AggregateDispatcher.cs</FileName>
250255
</TypeIdentifier>
251256
<ShowAsCollectionAssociation>
252-
<Field Name="sagas" />
257+
<Field Name="aggregates" />
253258
</ShowAsCollectionAssociation>
254259
<Lollipop Position="0.2" />
255260
</Class>
@@ -275,14 +280,14 @@
275280
</TypeIdentifier>
276281
</Interface>
277282
<Interface Name="SourceFlow.Aggregate.IAggregate" Collapsed="true">
278-
<Position X="14.5" Y="3.5" Width="1.75" />
283+
<Position X="15.5" Y="3.75" Width="1.75" />
279284
<TypeIdentifier>
280285
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
281286
<FileName>Aggregate\IAggregate.cs</FileName>
282287
</TypeIdentifier>
283288
</Interface>
284289
<Interface Name="SourceFlow.ViewModel.IProjection">
285-
<Position X="14.5" Y="4.75" Width="1.75" />
290+
<Position X="15.5" Y="5.5" Width="1.75" />
286291
<TypeIdentifier>
287292
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
288293
<FileName>ViewModel\IProjection.cs</FileName>
@@ -310,7 +315,7 @@
310315
</TypeIdentifier>
311316
</Interface>
312317
<Interface Name="SourceFlow.ViewModel.IProjectOn&lt;TEvent&gt;">
313-
<Position X="14.5" Y="6.25" Width="1.75" />
318+
<Position X="15.5" Y="6.75" Width="1.75" />
314319
<TypeIdentifier>
315320
<HashCode>AAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
316321
<FileName>ViewModel\IProjection.cs</FileName>
@@ -324,7 +329,7 @@
324329
</TypeIdentifier>
325330
</Interface>
326331
<Interface Name="SourceFlow.Aggregate.ISubscribes&lt;TEvent&gt;">
327-
<Position X="14.5" Y="2" Width="1.75" />
332+
<Position X="15.5" Y="2" Width="1.75" />
328333
<TypeIdentifier>
329334
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAA=</HashCode>
330335
<FileName>Aggregate\ISubscribes.cs</FileName>
@@ -352,28 +357,28 @@
352357
</TypeIdentifier>
353358
</Interface>
354359
<Interface Name="SourceFlow.Aggregate.IAggregateFactory">
355-
<Position X="16.5" Y="5.5" Width="1.75" />
360+
<Position X="17.75" Y="5.5" Width="1.75" />
356361
<TypeIdentifier>
357362
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAA=</HashCode>
358363
<FileName>Aggregate\IAggregateFactory.cs</FileName>
359364
</TypeIdentifier>
360365
</Interface>
361366
<Interface Name="SourceFlow.IViewProvider">
362-
<Position X="12.5" Y="7.5" Width="1.75" />
367+
<Position X="13.5" Y="8.25" Width="1.75" />
363368
<TypeIdentifier>
364369
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAACAA=</HashCode>
365370
<FileName>IViewProvider.cs</FileName>
366371
</TypeIdentifier>
367372
</Interface>
368373
<Interface Name="SourceFlow.ViewModel.IViewModel">
369-
<Position X="12.5" Y="6.25" Width="1.75" />
374+
<Position X="15.5" Y="8.25" Width="1.75" />
370375
<TypeIdentifier>
371376
<HashCode>AAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
372377
<FileName>ViewModel\IViewModel.cs</FileName>
373378
</TypeIdentifier>
374379
</Interface>
375380
<Interface Name="SourceFlow.Messaging.Bus.IEventDispatcher">
376-
<Position X="12.25" Y="2" Width="1.75" />
381+
<Position X="12.5" Y="2" Width="1.75" />
377382
<TypeIdentifier>
378383
<HashCode>AAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
379384
<FileName>Messaging\Bus\IEventDispatcher.cs</FileName>

src/SourceFlow/Impl/EventDispatcher.cs renamed to src/SourceFlow/Impl/AggregateDispatcher.cs

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Threading.Tasks;
54
using Microsoft.Extensions.Logging;
65
using SourceFlow.Aggregate;
76
using SourceFlow.Messaging;
87
using SourceFlow.Messaging.Bus;
9-
using SourceFlow.ViewModel;
108

119
namespace SourceFlow.Impl
1210
{
1311
/// <summary>
14-
/// EventDispatcher is responsible for dispatching events to the appropriate aggregates and view projections.
12+
/// This dispatcher is responsible for dispatching events to the appropriate subscribing aggregates.
1513
/// </summary>
16-
internal class EventDispatcher : IEventDispatcher
14+
internal class AggregateDispatcher : IEventDispatcher
1715
{
1816
/// <summary>
1917
/// Logger for the event queue to log events and errors.
2018
/// </summary>
2119
private readonly ILogger<IEventDispatcher> logger;
2220

23-
/// <summary>
24-
/// Represents a collection of view transforms used to modify or manipulate views.
25-
/// </summary>
26-
/// <remarks>This collection contains instances of objects implementing the <see
27-
/// cref="IProjection"/> interface. Each projection in the collection can be applied to alter the appearance
28-
29-
/// or behavior of a view.</remarks>
30-
private IEnumerable<IProjection> projections;
31-
3221
/// <summary>
3322
/// Represents a collection of aggregate root objects.
3423
/// </summary>
@@ -41,14 +30,12 @@ internal class EventDispatcher : IEventDispatcher
4130
/// Initializes a new instance of the <see cref="EventDispatcher"/> class with the specified aggregates and view projections.
4231
/// </summary>
4332
/// <param name="aggregates"></param>
44-
/// <param name="projections"></param>
4533
/// <param name="logger"></param>
4634
/// <exception cref="ArgumentNullException"></exception>
47-
public EventDispatcher(IEnumerable<IAggregate> aggregates, IEnumerable<IProjection> projections, ILogger<IEventDispatcher> logger)
35+
public AggregateDispatcher(IEnumerable<IAggregate> aggregates, ILogger<IEventDispatcher> logger)
4836
{
4937
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
5038
this.aggregates = aggregates ?? throw new ArgumentNullException(nameof(aggregates));
51-
this.projections = projections ?? throw new ArgumentNullException(nameof(projections));
5239
}
5340

5441
/// <summary>
@@ -57,7 +44,7 @@ public EventDispatcher(IEnumerable<IAggregate> aggregates, IEnumerable<IProjecti
5744
/// <typeparam name="TEvent"></typeparam>
5845
/// <param name="event"></param>
5946
/// <returns></returns>
60-
private async Task DequeueToAggregates<TEvent>(TEvent @event)
47+
private async Task Dispatch<TEvent>(TEvent @event)
6148
where TEvent : IEvent
6249
{
6350
var tasks = new List<Task>();
@@ -83,50 +70,14 @@ private async Task DequeueToAggregates<TEvent>(TEvent @event)
8370
await Task.WhenAll(tasks);
8471
}
8572

86-
/// <summary>
87-
/// Dequeues the event to all view projections that can handle it.
88-
/// </summary>
89-
/// <typeparam name="TEvent"></typeparam>
90-
/// <param name="event"></param>
91-
/// <returns></returns>
92-
public async Task DequeueToViews<TEvent>(TEvent @event)
93-
where TEvent : IEvent
94-
{
95-
var tasks = new List<Task>();
96-
97-
foreach (var projection in projections)
98-
{
99-
var projectionType = typeof(IProjectOn<>).MakeGenericType(@event.GetType());
100-
if (!projectionType.IsAssignableFrom(projection.GetType()))
101-
continue;
102-
103-
var method = typeof(IProjectOn<>)
104-
.MakeGenericType(@event.GetType())
105-
.GetMethod(nameof(IProjectOn<TEvent>.Apply));
106-
107-
var task = (Task)method.Invoke(projection, new object[] { @event });
108-
109-
tasks.Add(task);
110-
111-
logger?.LogInformation("Action=Event_Dispatcher_View, Event={Event}, Apply:{Apply}",
112-
@event.Name, projection.GetType().Name);
113-
}
114-
115-
if (!tasks.Any())
116-
return;
117-
118-
await Task.WhenAll(tasks);
119-
}
120-
12173
/// <summary>
12274
/// Dispatches the event to both aggregates and view projections.
12375
/// </summary>
12476
/// <param name="sender"></param>
12577
/// <param name="event"></param>
12678
public void Dispatch(object sender, IEvent @event)
12779
{
128-
DequeueToViews(@event).GetAwaiter().GetResult();
129-
DequeueToAggregates(@event).GetAwaiter().GetResult();
80+
Dispatch(@event).GetAwaiter().GetResult();
13081
logger?.LogInformation("Action=Event_Dispatcher_Complete, Event={Event}, Sender:{sender}",
13182
@event.Name, sender.GetType().Name);
13283
}

0 commit comments

Comments
 (0)