Skip to content

Commit d2afff9

Browse files
authored
Merge pull request #24533 from abpframework/BasicAggregateRoot_Events
Refactor BasicAggregateRoot to use nullable collections for distributed and local events
2 parents e66d88d + c8fa004 commit d2afff9

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/BasicAggregateRoot.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,38 @@ public abstract class BasicAggregateRoot : Entity,
1010
IAggregateRoot,
1111
IGeneratesDomainEvents
1212
{
13-
private readonly ICollection<DomainEventRecord> _distributedEvents = new Collection<DomainEventRecord>();
14-
private readonly ICollection<DomainEventRecord> _localEvents = new Collection<DomainEventRecord>();
13+
private ICollection<DomainEventRecord>? _distributedEvents;
14+
private ICollection<DomainEventRecord>? _localEvents;
1515

1616
public virtual IEnumerable<DomainEventRecord> GetLocalEvents()
1717
{
18-
return _localEvents;
18+
return _localEvents ?? Array.Empty<DomainEventRecord>();
1919
}
2020

2121
public virtual IEnumerable<DomainEventRecord> GetDistributedEvents()
2222
{
23-
return _distributedEvents;
23+
return _distributedEvents ?? Array.Empty<DomainEventRecord>();
2424
}
2525

2626
public virtual void ClearLocalEvents()
2727
{
28-
_localEvents.Clear();
28+
_localEvents?.Clear();
2929
}
3030

3131
public virtual void ClearDistributedEvents()
3232
{
33-
_distributedEvents.Clear();
33+
_distributedEvents?.Clear();
3434
}
3535

3636
protected virtual void AddLocalEvent(object eventData)
3737
{
38+
_localEvents ??= new Collection<DomainEventRecord>();
3839
_localEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext()));
3940
}
4041

4142
protected virtual void AddDistributedEvent(object eventData)
4243
{
44+
_distributedEvents ??= new Collection<DomainEventRecord>();
4345
_distributedEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext()));
4446
}
4547
}
@@ -49,8 +51,8 @@ public abstract class BasicAggregateRoot<TKey> : Entity<TKey>,
4951
IAggregateRoot<TKey>,
5052
IGeneratesDomainEvents
5153
{
52-
private readonly ICollection<DomainEventRecord> _distributedEvents = new Collection<DomainEventRecord>();
53-
private readonly ICollection<DomainEventRecord> _localEvents = new Collection<DomainEventRecord>();
54+
private ICollection<DomainEventRecord>? _distributedEvents;
55+
private ICollection<DomainEventRecord>? _localEvents;
5456

5557
protected BasicAggregateRoot()
5658
{
@@ -65,31 +67,33 @@ protected BasicAggregateRoot(TKey id)
6567

6668
public virtual IEnumerable<DomainEventRecord> GetLocalEvents()
6769
{
68-
return _localEvents;
70+
return _localEvents ?? Array.Empty<DomainEventRecord>();
6971
}
7072

7173
public virtual IEnumerable<DomainEventRecord> GetDistributedEvents()
7274
{
73-
return _distributedEvents;
75+
return _distributedEvents ?? Array.Empty<DomainEventRecord>();
7476
}
7577

7678
public virtual void ClearLocalEvents()
7779
{
78-
_localEvents.Clear();
80+
_localEvents?.Clear();
7981
}
8082

8183
public virtual void ClearDistributedEvents()
8284
{
83-
_distributedEvents.Clear();
85+
_distributedEvents?.Clear();
8486
}
8587

8688
protected virtual void AddLocalEvent(object eventData)
8789
{
90+
_localEvents ??= new Collection<DomainEventRecord>();
8891
_localEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext()));
8992
}
9093

9194
protected virtual void AddDistributedEvent(object eventData)
9295
{
96+
_distributedEvents ??= new Collection<DomainEventRecord>();
9397
_distributedEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext()));
9498
}
9599
}

0 commit comments

Comments
 (0)