-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
AggregateRoot.cs
48 lines (38 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
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using CompanyName.MyMeetings.BuildingBlocks.Domain;
namespace CompanyName.MyMeetings.Modules.Payments.Domain.SeedWork
{
public abstract class AggregateRoot
{
private readonly List<IDomainEvent> _domainEvents;
public Guid Id { get; protected set; }
public int Version { get; private set; }
public IReadOnlyCollection<IDomainEvent> GetDomainEvents() => _domainEvents.AsReadOnly();
protected void AddDomainEvent(IDomainEvent @event)
{
_domainEvents.Add(@event);
}
protected AggregateRoot()
{
_domainEvents = new List<IDomainEvent>();
Version = -1;
}
public void Load(IEnumerable<IDomainEvent> history)
{
foreach (var e in history)
{
Apply(e);
Version++;
}
}
protected abstract void Apply(IDomainEvent @event);
protected static void CheckRule(IBusinessRule rule)
{
if (rule.IsBroken())
{
throw new BusinessRuleValidationException(rule);
}
}
}
}