@@ -10,36 +10,48 @@ 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+ if ( _localEvents == null )
29+ {
30+ return ;
31+ }
32+
2833 _localEvents . Clear ( ) ;
2934 }
3035
3136 public virtual void ClearDistributedEvents ( )
3237 {
38+ if ( _distributedEvents == null )
39+ {
40+ return ;
41+ }
42+
3343 _distributedEvents . Clear ( ) ;
3444 }
3545
3646 protected virtual void AddLocalEvent ( object eventData )
3747 {
48+ _localEvents ??= new Collection < DomainEventRecord > ( ) ;
3849 _localEvents . Add ( new DomainEventRecord ( eventData , EventOrderGenerator . GetNext ( ) ) ) ;
3950 }
4051
4152 protected virtual void AddDistributedEvent ( object eventData )
4253 {
54+ _distributedEvents ??= new Collection < DomainEventRecord > ( ) ;
4355 _distributedEvents . Add ( new DomainEventRecord ( eventData , EventOrderGenerator . GetNext ( ) ) ) ;
4456 }
4557}
@@ -49,8 +61,8 @@ public abstract class BasicAggregateRoot<TKey> : Entity<TKey>,
4961 IAggregateRoot < TKey > ,
5062 IGeneratesDomainEvents
5163{
52- private readonly ICollection < DomainEventRecord > _distributedEvents = new Collection < DomainEventRecord > ( ) ;
53- private readonly ICollection < DomainEventRecord > _localEvents = new Collection < DomainEventRecord > ( ) ;
64+ private ICollection < DomainEventRecord > ? _distributedEvents ;
65+ private ICollection < DomainEventRecord > ? _localEvents ;
5466
5567 protected BasicAggregateRoot ( )
5668 {
@@ -65,31 +77,43 @@ protected BasicAggregateRoot(TKey id)
6577
6678 public virtual IEnumerable < DomainEventRecord > GetLocalEvents ( )
6779 {
68- return _localEvents ;
80+ return _localEvents ?? Array . Empty < DomainEventRecord > ( ) ;
6981 }
7082
7183 public virtual IEnumerable < DomainEventRecord > GetDistributedEvents ( )
7284 {
73- return _distributedEvents ;
85+ return _distributedEvents ?? Array . Empty < DomainEventRecord > ( ) ;
7486 }
7587
7688 public virtual void ClearLocalEvents ( )
7789 {
90+ if ( _localEvents == null )
91+ {
92+ return ;
93+ }
94+
7895 _localEvents . Clear ( ) ;
7996 }
8097
8198 public virtual void ClearDistributedEvents ( )
8299 {
100+ if ( _distributedEvents == null )
101+ {
102+ return ;
103+ }
104+
83105 _distributedEvents . Clear ( ) ;
84106 }
85107
86108 protected virtual void AddLocalEvent ( object eventData )
87109 {
110+ _localEvents ??= new Collection < DomainEventRecord > ( ) ;
88111 _localEvents . Add ( new DomainEventRecord ( eventData , EventOrderGenerator . GetNext ( ) ) ) ;
89112 }
90113
91114 protected virtual void AddDistributedEvent ( object eventData )
92115 {
116+ _distributedEvents ??= new Collection < DomainEventRecord > ( ) ;
93117 _distributedEvents . Add ( new DomainEventRecord ( eventData , EventOrderGenerator . GetNext ( ) ) ) ;
94118 }
95119}
0 commit comments