-
Notifications
You must be signed in to change notification settings - Fork 30
/
TransactionalOutbox.cs
executable file
·43 lines (34 loc) · 1.5 KB
/
TransactionalOutbox.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
using MyCompany.ECommerce.TechnicalStuff.ProcessModel;
namespace MyCompany.ECommerce.TechnicalStuff.Outbox;
public class TransactionalOutbox
{
private readonly List<OutboxMessage> _messages = new();
private readonly TransactionalOutboxRepository _repository;
protected TransactionalOutbox(TransactionalOutboxes outboxes, TransactionalOutboxRepository repository)
{
outboxes.Register(this);
_repository = repository;
}
public Task Save() => _repository.Save(_messages);
protected void Add(string partitionKey, string processorType, string messageTypeId, string payload) =>
_messages.Add(new OutboxMessage(Guid.NewGuid(), partitionKey, processorType, messageTypeId, payload));
}
public abstract class TransactionalOutbox<TMessage>(
TransactionalOutboxes outboxes,
TransactionalOutboxRepository repository,
MessageTypes messageTypes)
: TransactionalOutbox(outboxes, repository)
where TMessage : Message
{
public void Add(TMessage message)
{
var partitionKey = GetPartitionKeyFor(message);
var processorType = GetProcessorTypeFor(message);
var messageTypeId = messageTypes.GetTypeIdFor(message);
var payload = CreatePayloadFrom(message);
Add(partitionKey, processorType, messageTypeId, payload);
}
protected abstract string GetPartitionKeyFor(TMessage message);
protected abstract string GetProcessorTypeFor(TMessage message);
protected abstract string CreatePayloadFrom(TMessage message);
}