Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit f2a4f3a

Browse files
authored
Merge pull request #561 from Alexbits/hotfix/EntityEncapsulationFix
Improved encapsulation of DomainEvents collection in Entity class
2 parents 0c7671e + 1d8c617 commit f2a4f3a

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/Services/Ordering/Ordering.Domain/SeedWork/Entity.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,22 @@ protected set
2121
}
2222

2323
private List<INotification> _domainEvents;
24-
public List<INotification> DomainEvents => _domainEvents;
25-
24+
public IReadOnlyCollection<INotification> DomainEvents => _domainEvents?.AsReadOnly();
25+
2626
public void AddDomainEvent(INotification eventItem)
2727
{
2828
_domainEvents = _domainEvents ?? new List<INotification>();
2929
_domainEvents.Add(eventItem);
3030
}
31+
3132
public void RemoveDomainEvent(INotification eventItem)
3233
{
33-
if (_domainEvents is null) return;
34-
_domainEvents.Remove(eventItem);
34+
_domainEvents?.Remove(eventItem);
35+
}
36+
37+
public void ClearDomainEvents()
38+
{
39+
_domainEvents?.Clear();
3540
}
3641

3742
public bool IsTransient()

src/Services/Ordering/Ordering.Infrastructure/MediatorExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static async Task DispatchDomainEventsAsync(this IMediator mediator, Orde
1919
.ToList();
2020

2121
domainEntities.ToList()
22-
.ForEach(entity => entity.Entity.DomainEvents.Clear());
22+
.ForEach(entity => entity.Entity.ClearDomainEvents());
2323

2424
var tasks = domainEvents
2525
.Select(async (domainEvent) => {

0 commit comments

Comments
 (0)