Skip to content

Commit 2f81ca8

Browse files
committed
- Checkpoint: Refactor Event and Command Contract
1 parent f84c9a2 commit 2f81ca8

22 files changed

Lines changed: 179 additions & 171 deletions

src/SourceFlow.ConsoleApp/Impl/InMemoryEventStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public Task<int> GetNextSequenceNo(int aggregateId)
2828
{
2929
if (_store.TryGetValue(aggregateId, out var events))
3030
{
31-
return Task.FromResult(events.Max<ICommand, int>(c => c.SequenceNo) + 1);
31+
return Task.FromResult(events.Max<ICommand, int>(c => ((IMetadata)c).Metadata.SequenceNo) + 1);
3232
}
3333
return Task.FromResult(1);
3434
}

src/SourceFlow.ConsoleApp/Sagas/AccountSaga.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using SourceFlow.ConsoleApp.Aggregates;
33
using SourceFlow.ConsoleApp.Commands;
44
using SourceFlow.ConsoleApp.Events;
5-
using SourceFlow.Messaging;
65
using SourceFlow.Saga;
76

87
namespace SourceFlow.ConsoleApp.Sagas

src/SourceFlow.ConsoleApp/Services/IAccountService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using SourceFlow.ConsoleApp.Aggregates;
2-
31
namespace SourceFlow.ConsoleApp.Services
42
{
53
public interface IAccountService

src/SourceFlow/Aggregate/IAggregateFactory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Threading.Tasks;
32

43
namespace SourceFlow.Aggregate

src/SourceFlow/Aggregate/IEntity.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
31
namespace SourceFlow.Aggregate
42
{
53
public interface IEntity

src/SourceFlow/IEventStore.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Threading.Tasks;
43
using SourceFlow.Messaging;

src/SourceFlow/Impl/CommandBus.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,18 @@ private async Task PublishToSagas<TCommand>(TCommand command) where TCommand : I
9090
private async Task SagaHandle<TCommand>(ISaga saga, TCommand command) where TCommand : ICommand
9191
{
9292
// 1. Set event sequence no.
93-
if (!command.IsReplay)
94-
command.SequenceNo = await eventStore.GetNextSequenceNo(command.Payload.Id);
93+
if (!((IMetadata)command).Metadata.IsReplay)
94+
((IMetadata)command).Metadata.SequenceNo = await eventStore.GetNextSequenceNo(command.Payload.Id);
9595

9696
// 4. Log event.
9797
logger?.LogInformation("Action=Command_Dispatched, Command={Command}, Payload={Payload}, SequenceNo={No}, Saga={Saga}",
98-
command.GetType().Name, command.Payload.GetType().Name, command.SequenceNo, saga.GetType().Name);
98+
command.GetType().Name, command.Payload.GetType().Name, ((IMetadata)command).Metadata.SequenceNo, saga.GetType().Name);
9999

100100
// 2. handle event by Saga?
101101
await saga.Handle(command);
102102

103103
// 3. When event is not replayed
104-
if (!command.IsReplay)
104+
if (!((IMetadata)command).Metadata.IsReplay)
105105
// 3.1. Append event to event store.
106106
await eventStore.Append(command);
107107
}
@@ -120,7 +120,7 @@ async Task ICommandBus.Replay(int aggregateId)
120120

121121
foreach (var command in commands.ToList())
122122
{
123-
command.IsReplay = true;
123+
((IMetadata)command).Metadata.IsReplay = true;
124124
await PublishToSagas(command);
125125
}
126126
}

src/SourceFlow/Impl/CommandReplayer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Threading.Tasks;
32
using SourceFlow.Messaging.Bus;
43

src/SourceFlow/Messaging/BaseCommand.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
using System;
2-
31
namespace SourceFlow.Messaging
42
{
53
/// <summary>
64
/// Base class for command in the command-driven architecture.
75
/// </summary>
8-
public class BaseCommand<TPayload> : ICommand<TPayload>
6+
public class BaseCommand<TPayload> : ICommand
97
where TPayload : class, IPayload, new()
108
{
119
/// <summary>
@@ -14,30 +12,20 @@ public class BaseCommand<TPayload> : ICommand<TPayload>
1412
/// <param name="payload"></param>
1513
public BaseCommand(TPayload payload)
1614
{
17-
EventId = Guid.NewGuid();
18-
OccurredOn = DateTime.UtcNow;
15+
Metadata = new Metadata();
16+
Name = GetType().Name;
1917
Payload = payload;
2018
}
2119

2220
/// <summary>
23-
/// Unique identifier for the command.
24-
/// </summary>
25-
public Guid EventId { get; }
26-
27-
/// <summary>
28-
/// Indicates whether the command is a replay of an existing command.
29-
/// </summary>
30-
public DateTime OccurredOn { get; }
31-
32-
/// <summary>
33-
/// Indicates whether the command is a replay of an existing command.
21+
/// Metadata associated with the command, which includes information such as event ID, occurrence time, and sequence number.
3422
/// </summary>
35-
bool ICommand.IsReplay { get; set; }
23+
public Metadata Metadata { get; set; } = new Metadata();
3624

3725
/// <summary>
38-
/// Sequence number of the command within the aggregate's command stream.
26+
/// Name of the command, typically the class name.
3927
/// </summary>
40-
public int SequenceNo { get; set; }
28+
public string Name { get; set; }
4129

4230
/// <summary>
4331
/// Payload of the command, containing the data associated with the command.

src/SourceFlow/Messaging/BaseEvent.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ namespace SourceFlow.Messaging
99
public abstract class BaseEvent<TEntity> : IEvent
1010
where TEntity : IEntity
1111
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="BaseEvent{TEntity}"/> class with a specified payload.
14+
/// </summary>
15+
/// <param name="payload"></param>
16+
public BaseEvent(TEntity payload)
17+
{
18+
Metadata = new Metadata();
19+
Name = GetType().Name;
20+
Payload = payload;
21+
}
22+
23+
/// <summary>
24+
/// Metadata associated with the event, which includes information such as event ID, occurrence time, and sequence number.
25+
/// </summary>
26+
public Metadata Metadata { get; set; }
27+
1228
/// <summary>
1329
/// Name of the event, typically the class name.
1430
/// </summary>
@@ -30,15 +46,5 @@ IEntity IEvent.Payload
3046
Payload = (TEntity)value;
3147
}
3248
}
33-
34-
/// <summary>
35-
/// Creates a new instance of the <see cref="BaseEvent{TEntity}"/> class with the specified payload.
36-
/// </summary>
37-
/// <param name="payload"></param>
38-
public BaseEvent(TEntity payload)
39-
{
40-
Name = GetType().Name;
41-
Payload = payload;
42-
}
4349
}
4450
}

0 commit comments

Comments
 (0)