-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAggregateRoot.cs
78 lines (67 loc) · 2.43 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using Shared;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Domain
{
public abstract class AggregateRoot : IAggregateRoot
{
public TenantId TenantId { get; set; }
private readonly List<Event> _changes = new List<Event>();
// Contains any business rules that were violated while processing a command
// These broken rules are not persisted in the event store but used by the
// command handler(s) to further deal with them.
private List<BrokenRule> _brokenRules = new List<BrokenRule>();
public int Version { get; set; } = -1;
public List<BrokenRule> GetBrokenRules()
{
return _brokenRules;
}
public void AddBrokenRule(BrokenRule brokenRule)
{
if (!_brokenRules.Contains(brokenRule))
{
_brokenRules.Add(brokenRule);
Console.WriteLine($"Added broken rule #{brokenRule.Type.Value}, {brokenRule.Type.DisplayName}, {brokenRule.Type.Description}");
}
}
/// <summary>
/// Child aggregates can further handle the clearing of the rules
/// before they are cleared by the parent.
/// </summary>
protected void ResolveBrokenRules()
{
_brokenRules.Clear();
}
public IEnumerable<Event> GetUncommittedChanges()
{
return _changes;
}
public void MarkChangesAsCommitted()
{
_changes.Clear();
ResolveBrokenRules();
}
public void LoadsFromHistory(IList<Event> history)
{
for (int i = 0; i < history.Count; i++)
{
ApplyChange(history[i], false);
}
}
protected void ApplyChange(Event @event)
{
TenantId = @event.TenantId;
ApplyChange(@event, true);
}
// push atomic aggregate changes to local history for further processing (MemoryEventStore.SaveEvents)
private void ApplyChange(Event @event, bool isNew)
{
Type[] types = { @event.GetType() };
MethodInfo dynMethod = this.GetType().GetMethod("Apply", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);
dynMethod.Invoke(this, new object[] { @event });
if (isNew) _changes.Add(@event);
Version++;
}
}
}