-
Notifications
You must be signed in to change notification settings - Fork 0
/
AggregateRoot.cs
40 lines (33 loc) · 1.18 KB
/
AggregateRoot.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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MKES.Interfaces;
using MKES.Model;
namespace MKES
{
public abstract class AggregateRoot
{
private readonly IEventStoreRepository _eventStoreRepository;
private List<Event> _uncommitedChanges = new List<Event>();
private Dictionary<Type, dynamic> _changes = new Dictionary<Type, dynamic>();
public IReadOnlyCollection<Event> GetUncommitedChanges() => _uncommitedChanges.AsReadOnly();
public AggregateRoot(IEventStoreRepository eventStoreRepository)
{
_eventStoreRepository = eventStoreRepository;
}
public async Task CommitChanges()
{
await _eventStoreRepository.Commit(_uncommitedChanges);
_uncommitedChanges = new List<Event>();
}
protected void Register<TEvent>(Action<TEvent> aggregate) where TEvent : Event, new()
{
_changes.Add(typeof(TEvent), aggregate);
}
protected void ApplyChanges<T>(T @event) where T: Event
{
_uncommitedChanges.Add(@event);
_changes[typeof(T)].Invoke(@event);
}
}
}