From 1e253b484452f04c80387e5b0af8b24ea9e1fed7 Mon Sep 17 00:00:00 2001 From: maliming Date: Sun, 4 Jan 2026 18:24:46 +0800 Subject: [PATCH] Refactor BasicAggregateRoot to use nullable collections for distributed and local events --- .../Abp/Domain/Entities/BasicAggregateRoot.cs | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/BasicAggregateRoot.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/BasicAggregateRoot.cs index e028b55844..826233d5a3 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/BasicAggregateRoot.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/BasicAggregateRoot.cs @@ -10,36 +10,48 @@ public abstract class BasicAggregateRoot : Entity, IAggregateRoot, IGeneratesDomainEvents { - private readonly ICollection _distributedEvents = new Collection(); - private readonly ICollection _localEvents = new Collection(); + private ICollection? _distributedEvents; + private ICollection? _localEvents; public virtual IEnumerable GetLocalEvents() { - return _localEvents; + return _localEvents ?? Array.Empty(); } public virtual IEnumerable GetDistributedEvents() { - return _distributedEvents; + return _distributedEvents ?? Array.Empty(); } public virtual void ClearLocalEvents() { + if (_localEvents == null) + { + return; + } + _localEvents.Clear(); } public virtual void ClearDistributedEvents() { + if (_distributedEvents == null) + { + return; + } + _distributedEvents.Clear(); } protected virtual void AddLocalEvent(object eventData) { + _localEvents ??= new Collection(); _localEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext())); } protected virtual void AddDistributedEvent(object eventData) { + _distributedEvents ??= new Collection(); _distributedEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext())); } } @@ -49,8 +61,8 @@ public abstract class BasicAggregateRoot : Entity, IAggregateRoot, IGeneratesDomainEvents { - private readonly ICollection _distributedEvents = new Collection(); - private readonly ICollection _localEvents = new Collection(); + private ICollection? _distributedEvents; + private ICollection? _localEvents; protected BasicAggregateRoot() { @@ -65,31 +77,43 @@ public abstract class BasicAggregateRoot : Entity, public virtual IEnumerable GetLocalEvents() { - return _localEvents; + return _localEvents ?? Array.Empty(); } public virtual IEnumerable GetDistributedEvents() { - return _distributedEvents; + return _distributedEvents ?? Array.Empty(); } public virtual void ClearLocalEvents() { + if (_localEvents == null) + { + return; + } + _localEvents.Clear(); } public virtual void ClearDistributedEvents() { + if (_distributedEvents == null) + { + return; + } + _distributedEvents.Clear(); } protected virtual void AddLocalEvent(object eventData) { + _localEvents ??= new Collection(); _localEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext())); } protected virtual void AddDistributedEvent(object eventData) { + _distributedEvents ??= new Collection(); _distributedEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext())); } }