-
Notifications
You must be signed in to change notification settings - Fork 154
/
EventListener.cs
45 lines (38 loc) · 1.17 KB
/
EventListener.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
using DddInPractice.Logic.Common;
using NHibernate.Event;
namespace DddInPractice.Logic.Utils
{
internal class EventListener :
IPostInsertEventListener,
IPostDeleteEventListener,
IPostUpdateEventListener,
IPostCollectionUpdateEventListener
{
public void OnPostUpdate(PostUpdateEvent ev)
{
DispatchEvents(ev.Entity as AggregateRoot);
}
public void OnPostDelete(PostDeleteEvent ev)
{
DispatchEvents(ev.Entity as AggregateRoot);
}
public void OnPostInsert(PostInsertEvent ev)
{
DispatchEvents(ev.Entity as AggregateRoot);
}
public void OnPostUpdateCollection(PostCollectionUpdateEvent ev)
{
DispatchEvents(ev.AffectedOwnerOrNull as AggregateRoot);
}
private void DispatchEvents(AggregateRoot aggregateRoot)
{
if (aggregateRoot == null)
return;
foreach (IDomainEvent domainEvent in aggregateRoot.DomainEvents)
{
DomainEvents.Dispatch(domainEvent);
}
aggregateRoot.ClearEvents();
}
}
}