-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomainEventsAccessor.cs
More file actions
35 lines (29 loc) · 1.07 KB
/
DomainEventsAccessor.cs
File metadata and controls
35 lines (29 loc) · 1.07 KB
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
using Microsoft.EntityFrameworkCore;
namespace EasyWay.Internals.DomainEvents
{
internal sealed class DomainEventsAccessor : IDomainEventsContext
{
private readonly DbContext _dbContext;
public DomainEventsAccessor(DbContext dbContext)
{
_dbContext = dbContext;
}
public IReadOnlyCollection<DomainEventContext> GetAllDomainEvents()
{
var domainEntities = _dbContext.ChangeTracker
.Entries<Entity>()
.Where(x => x.Entity.DomainEvents != null && x.Entity.DomainEvents.Any()).ToList();
return domainEntities
.SelectMany(x => x.Entity.DomainEvents)
.ToList();
}
public void ClearAllDomainEvents()
{
var domainEntities = _dbContext.ChangeTracker
.Entries<Entity>()
.Where(x => x.Entity.DomainEvents != null && x.Entity.DomainEvents.Any()).ToList();
domainEntities
.ForEach(entity => entity.Entity.ClearDomainEvents());
}
}
}