Skip to content

Commit 0c09ee4

Browse files
committed
Used EventStore instead of CommandBus in Event Sourced business processes solution
1 parent b5cf59f commit 0c09ee4

File tree

16 files changed

+41
-58
lines changed

16 files changed

+41
-58
lines changed

Workshops/IntroductionToEventSourcing/16-EntitiesDefinition/Core/EventStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async ValueTask AppendToStream(string streamId, object[] toAppend, Cancel
4242
public ValueTask<TEvent[]> ReadStream<TEvent>(string streamId, CancellationToken ct = default)
4343
where TEvent : notnull =>
4444
ValueTask.FromResult(events.TryGetValue(streamId, out var stream)
45-
? stream.Select(e => e.Data).Cast<TEvent>().ToArray()
45+
? stream.Select(e => e.Data).OfType<TEvent>().ToArray()
4646
: []);
4747

4848
public EventStore Subscribe<T>(Func<T, CancellationToken, ValueTask> eventHandler)

Workshops/IntroductionToEventSourcing/17-BusinessProcesses/Core/EventStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async ValueTask AppendToStream(string streamId, object[] toAppend, Cancel
4242
public ValueTask<TEvent[]> ReadStream<TEvent>(string streamId, CancellationToken ct = default)
4343
where TEvent : notnull =>
4444
ValueTask.FromResult(events.TryGetValue(streamId, out var stream)
45-
? stream.Select(e => e.Data).Cast<TEvent>().ToArray()
45+
? stream.Select(e => e.Data).OfType<TEvent>().ToArray()
4646
: []);
4747

4848
public EventStore Subscribe<T>(Func<T, CancellationToken, ValueTask> eventHandler)

Workshops/IntroductionToEventSourcing/Solved/16-EntitiesDefinition/Core/EventStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async ValueTask AppendToStream(string streamId, object[] toAppend, Cancel
4242
public ValueTask<TEvent[]> ReadStream<TEvent>(string streamId, CancellationToken ct = default)
4343
where TEvent : notnull =>
4444
ValueTask.FromResult(events.TryGetValue(streamId, out var stream)
45-
? stream.Select(e => e.Data).Cast<TEvent>().ToArray()
45+
? stream.Select(e => e.Data).OfType<TEvent>().ToArray()
4646
: []);
4747

4848
public EventStore Subscribe<T>(Func<T, CancellationToken, ValueTask> eventHandler)

Workshops/IntroductionToEventSourcing/Solved/17-BusinessProcesses/Choreography/BusinessProcessTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ public async Task GroupCheckoutForMultipleGuestStayWithAllUnsettled_ShouldFail()
207207
}
208208

209209
private readonly EventStore eventStore = new();
210-
private readonly CommandBus commandBus = new();
211210
private readonly MessageCatcher publishedMessages = new();
212211
private readonly GuestStayFacade guestStayFacade;
213212
private readonly GroupCheckOutFacade groupCheckoutFacade;
@@ -219,12 +218,11 @@ public BusinessProcessTests(ITestOutputHelper testOutputHelper)
219218
{
220219
this.testOutputHelper = testOutputHelper;
221220
guestStayFacade = new GuestStayFacade(eventStore);
222-
groupCheckoutFacade = new GroupCheckOutFacade(eventStore, commandBus);
221+
groupCheckoutFacade = new GroupCheckOutFacade(eventStore);
223222

224223
eventStore.Use(publishedMessages.Catch);
225-
commandBus.Use(publishedMessages.Catch);
226224

227225
ConfigureGroupCheckouts(eventStore, groupCheckoutFacade);
228-
ConfigureGuestStayAccounts(commandBus, guestStayFacade);
226+
ConfigureGuestStayAccounts(eventStore, guestStayFacade);
229227
}
230228
}

Workshops/IntroductionToEventSourcing/Solved/17-BusinessProcesses/Choreography/GroupCheckouts/GroupCheckoutFacade.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace BusinessProcesses.Choreography.GroupCheckouts;
77
using static GuestStayAccountEvent;
88
using static GuestStayAccountCommand;
99

10-
public class GroupCheckOutFacade(EventStore eventStore, CommandBus commandBus)
10+
public class GroupCheckOutFacade(EventStore eventStore)
1111
{
1212
public async ValueTask InitiateGroupCheckout(InitiateGroupCheckout command, CancellationToken ct = default)
1313
{
@@ -19,7 +19,7 @@ public async ValueTask InitiateGroupCheckout(InitiateGroupCheckout command, Canc
1919

2020
public ValueTask GroupCheckoutInitiated(GroupCheckoutEvent.GroupCheckoutInitiated @event,
2121
CancellationToken ct = default) =>
22-
commandBus.Send(
22+
eventStore.AppendToStream("commands",
2323
[
2424
..@event.GuestStayIds
2525
.Select(guestStayId => new CheckOutGuest(guestStayId, @event.InitiatedAt, @event.GroupCheckoutId))

Workshops/IntroductionToEventSourcing/Solved/17-BusinessProcesses/Choreography/GuestStayAccounts/GroupCheckoutsConfig.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ namespace BusinessProcesses.Choreography.GuestStayAccounts;
77
public static class GuestStayAccountsConfig
88
{
99
public static void ConfigureGuestStayAccounts(
10-
CommandBus commandBus,
10+
EventStore eventStore,
1111
GuestStayFacade guestStayFacade
1212
)
1313
{
14-
commandBus.Handle<CheckOutGuest>(guestStayFacade.CheckOutGuest);
14+
eventStore.Subscribe<CheckOutGuest>(guestStayFacade.CheckOutGuest);
1515
}
1616
}

Workshops/IntroductionToEventSourcing/Solved/17-BusinessProcesses/Core/EventStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async ValueTask AppendToStream(string streamId, object[] toAppend, Cancel
4242
public ValueTask<TEvent[]> ReadStream<TEvent>(string streamId, CancellationToken ct = default)
4343
where TEvent : notnull =>
4444
ValueTask.FromResult(events.TryGetValue(streamId, out var stream)
45-
? stream.Select(e => e.Data).Cast<TEvent>().ToArray()
45+
? stream.Select(e => e.Data).OfType<TEvent>().ToArray()
4646
: []);
4747

4848
public EventStore Subscribe<T>(Func<T, CancellationToken, ValueTask> eventHandler)

Workshops/IntroductionToEventSourcing/Solved/17-BusinessProcesses/ProcessManagers/BusinessProcessTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ public async Task GroupCheckoutForMultipleGuestStayWithAllUnsettled_ShouldFail()
207207
}
208208

209209
private readonly EventStore eventStore = new();
210-
private readonly CommandBus commandBus = new();
211210
private readonly MessageCatcher publishedMessages = new();
212211
private readonly GuestStayFacade guestStayFacade;
213212
private readonly GroupCheckOutFacade groupCheckoutFacade;
@@ -219,12 +218,11 @@ public BusinessProcessTests(ITestOutputHelper testOutputHelper)
219218
{
220219
this.testOutputHelper = testOutputHelper;
221220
guestStayFacade = new GuestStayFacade(eventStore);
222-
groupCheckoutFacade = new GroupCheckOutFacade(eventStore, commandBus);
221+
groupCheckoutFacade = new GroupCheckOutFacade(eventStore);
223222

224223
eventStore.Use(publishedMessages.Catch);
225-
commandBus.Use(publishedMessages.Catch);
226224

227225
ConfigureGroupCheckouts(eventStore, groupCheckoutFacade);
228-
ConfigureGuestStayAccounts(commandBus, guestStayFacade);
226+
ConfigureGuestStayAccounts(eventStore, guestStayFacade);
229227
}
230228
}

Workshops/IntroductionToEventSourcing/Solved/17-BusinessProcesses/ProcessManagers/GroupCheckouts/GroupCheckoutFacade.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace BusinessProcesses.ProcessManagers.GroupCheckouts;
77
using static GuestStayAccountEvent;
88
using static ProcessManagerResult;
99

10-
public class GroupCheckOutFacade(EventStore eventStore, CommandBus commandBus)
10+
public class GroupCheckOutFacade(EventStore eventStore)
1111
{
1212
public async ValueTask InitiateGroupCheckout(InitiateGroupCheckout command, CancellationToken ct = default)
1313
{
@@ -21,7 +21,7 @@ public async ValueTask GuestCheckedOut(GuestCheckedOut @event, CancellationToken
2121
if (!@event.GroupCheckOutId.HasValue)
2222
return;
2323

24-
var groupCheckout = await GetGroupCheckOut(@event.GroupCheckOutId.Value, ct);
24+
var groupCheckout = await GetGroupCheckOut(@event.GroupCheckOutId.Value, ct);
2525

2626
var messages = groupCheckout.On(@event);
2727

@@ -33,7 +33,7 @@ public async ValueTask GuestCheckOutFailed(GuestCheckOutFailed @event, Cancellat
3333
if (!@event.GroupCheckOutId.HasValue)
3434
return;
3535

36-
var groupCheckout = await GetGroupCheckOut(@event.GroupCheckOutId.Value, ct);
36+
var groupCheckout = await GetGroupCheckOut(@event.GroupCheckOutId.Value, ct);
3737

3838
var messages = groupCheckout.On(@event);
3939

@@ -47,18 +47,11 @@ private async Task ProcessMessages(
4747
CancellationToken ct
4848
)
4949
{
50-
foreach (var message in messages)
51-
{
52-
switch (message)
53-
{
54-
case Event (GroupCheckoutEvent @event):
55-
await eventStore.AppendToStream(groupCheckOutId.ToString(), [@event], ct);
56-
break;
57-
case Command(GuestStayAccountCommand command):
58-
await commandBus.Send([command], ct);
59-
break;
60-
}
61-
}
50+
await eventStore.AppendToStream(
51+
groupCheckOutId.ToString(),
52+
messages.Where(m => m is not None).Select(m => m is Event @event ? @event.Message : ((Command)m).Message).ToArray(),
53+
ct
54+
);
6255
}
6356

6457
private async ValueTask<GroupCheckOut> GetGroupCheckOut(Guid guestStayId, CancellationToken ct)
@@ -70,7 +63,7 @@ private async ValueTask<GroupCheckOut> GetGroupCheckOut(Guid guestStayId, Cancel
7063
ct
7164
);
7265

73-
if(groupCheckout == GroupCheckOut.Initial)
66+
if (groupCheckout == GroupCheckOut.Initial)
7467
throw new InvalidOperationException("Entity not found");
7568

7669
return groupCheckout;

Workshops/IntroductionToEventSourcing/Solved/17-BusinessProcesses/ProcessManagers/GuestStayAccounts/GroupCheckoutsConfig.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ namespace BusinessProcesses.ProcessManagers.GuestStayAccounts;
77
public static class GuestStayAccountsConfig
88
{
99
public static void ConfigureGuestStayAccounts(
10-
CommandBus commandBus,
10+
EventStore eventStore,
1111
GuestStayFacade guestStayFacade
1212
)
1313
{
14-
commandBus.Handle<CheckOutGuest>(guestStayFacade.CheckOutGuest);
14+
eventStore.Subscribe<CheckOutGuest>(guestStayFacade.CheckOutGuest);
1515
}
1616
}

0 commit comments

Comments
 (0)