Browse Source

Refactor BasicAggregateRoot to use nullable collections for distributed and local events

pull/24533/head
maliming 9 months ago
parent
commit
1e253b4844
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 40
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/BasicAggregateRoot.cs

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

@ -10,36 +10,48 @@ public abstract class BasicAggregateRoot : Entity,
IAggregateRoot, IAggregateRoot,
IGeneratesDomainEvents IGeneratesDomainEvents
{ {
private readonly ICollection<DomainEventRecord> _distributedEvents = new Collection<DomainEventRecord>(); private ICollection<DomainEventRecord>? _distributedEvents;
private readonly ICollection<DomainEventRecord> _localEvents = new Collection<DomainEventRecord>(); private ICollection<DomainEventRecord>? _localEvents;
public virtual IEnumerable<DomainEventRecord> GetLocalEvents() public virtual IEnumerable<DomainEventRecord> GetLocalEvents()
{ {
return _localEvents; return _localEvents ?? Array.Empty<DomainEventRecord>();
} }
public virtual IEnumerable<DomainEventRecord> GetDistributedEvents() public virtual IEnumerable<DomainEventRecord> GetDistributedEvents()
{ {
return _distributedEvents; return _distributedEvents ?? Array.Empty<DomainEventRecord>();
} }
public virtual void ClearLocalEvents() public virtual void ClearLocalEvents()
{ {
if (_localEvents == null)
{
return;
}
_localEvents.Clear(); _localEvents.Clear();
} }
public virtual void ClearDistributedEvents() public virtual void ClearDistributedEvents()
{ {
if (_distributedEvents == null)
{
return;
}
_distributedEvents.Clear(); _distributedEvents.Clear();
} }
protected virtual void AddLocalEvent(object eventData) protected virtual void AddLocalEvent(object eventData)
{ {
_localEvents ??= new Collection<DomainEventRecord>();
_localEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext())); _localEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext()));
} }
protected virtual void AddDistributedEvent(object eventData) protected virtual void AddDistributedEvent(object eventData)
{ {
_distributedEvents ??= new Collection<DomainEventRecord>();
_distributedEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext())); _distributedEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext()));
} }
} }
@ -49,8 +61,8 @@ public abstract class BasicAggregateRoot<TKey> : Entity<TKey>,
IAggregateRoot<TKey>, IAggregateRoot<TKey>,
IGeneratesDomainEvents IGeneratesDomainEvents
{ {
private readonly ICollection<DomainEventRecord> _distributedEvents = new Collection<DomainEventRecord>(); private ICollection<DomainEventRecord>? _distributedEvents;
private readonly ICollection<DomainEventRecord> _localEvents = new Collection<DomainEventRecord>(); private ICollection<DomainEventRecord>? _localEvents;
protected BasicAggregateRoot() protected BasicAggregateRoot()
{ {
@ -65,31 +77,43 @@ public abstract class BasicAggregateRoot<TKey> : Entity<TKey>,
public virtual IEnumerable<DomainEventRecord> GetLocalEvents() public virtual IEnumerable<DomainEventRecord> GetLocalEvents()
{ {
return _localEvents; return _localEvents ?? Array.Empty<DomainEventRecord>();
} }
public virtual IEnumerable<DomainEventRecord> GetDistributedEvents() public virtual IEnumerable<DomainEventRecord> GetDistributedEvents()
{ {
return _distributedEvents; return _distributedEvents ?? Array.Empty<DomainEventRecord>();
} }
public virtual void ClearLocalEvents() public virtual void ClearLocalEvents()
{ {
if (_localEvents == null)
{
return;
}
_localEvents.Clear(); _localEvents.Clear();
} }
public virtual void ClearDistributedEvents() public virtual void ClearDistributedEvents()
{ {
if (_distributedEvents == null)
{
return;
}
_distributedEvents.Clear(); _distributedEvents.Clear();
} }
protected virtual void AddLocalEvent(object eventData) protected virtual void AddLocalEvent(object eventData)
{ {
_localEvents ??= new Collection<DomainEventRecord>();
_localEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext())); _localEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext()));
} }
protected virtual void AddDistributedEvent(object eventData) protected virtual void AddDistributedEvent(object eventData)
{ {
_distributedEvents ??= new Collection<DomainEventRecord>();
_distributedEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext())); _distributedEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext()));
} }
} }

Loading…
Cancel
Save