Class AggregateRoot<TId>
- Namespace
- Compendium.Core.Domain.Primitives
- Assembly
- Compendium.Core.dll
Base class for aggregate roots in the domain. Provides domain event management and optimistic concurrency control.
public abstract class AggregateRoot<TId> : Entity<TId>, IEquatable<Entity<TId>>, IDisposable where TId : notnull
Type Parameters
TIdThe type of the aggregate root identifier.
- Inheritance
-
Entity<TId>AggregateRoot<TId>
- Implements
-
IEquatable<Entity<TId>>
- Inherited Members
Constructors
AggregateRoot(TId, ILockingStrategy?)
Initializes a new instance of the AggregateRoot<TId> class.
protected AggregateRoot(TId id, ILockingStrategy? lockingStrategy = null)
Parameters
idTIdThe aggregate root identifier.
lockingStrategyILockingStrategyThe locking strategy to use. If null, uses ReaderWriterLockStrategy.
Properties
DomainEvents
Gets the collection of uncommitted domain events. Uses frozen collections for efficient reads and caching.
public IReadOnlyCollection<IDomainEvent> DomainEvents { get; }
Property Value
HasDomainEvents
Gets a value indicating whether there are uncommitted domain events.
public bool HasDomainEvents { get; }
Property Value
Version
Gets the version for optimistic concurrency control.
public long Version { get; }
Property Value
Methods
AddDomainEvent(IDomainEvent)
Adds a domain event to the aggregate. Prevents duplicate events using content-based deduplication.
protected void AddDomainEvent(IDomainEvent domainEvent)
Parameters
domainEventIDomainEventThe domain event to add.
ClearDomainEvents()
Clears all domain events from the aggregate.
public void ClearDomainEvents()
Dispose()
Disposes the aggregate root and its resources.
public void Dispose()
Dispose(bool)
Disposes the aggregate root and its resources.
protected virtual void Dispose(bool disposing)
Parameters
disposingboolTrue if disposing managed resources.
GetUncommittedEvents()
Gets all uncommitted domain events and clears the collection.
public IReadOnlyCollection<IDomainEvent> GetUncommittedEvents()
Returns
- IReadOnlyCollection<IDomainEvent>
The collection of uncommitted domain events.
IncrementVersion()
Increments the version for optimistic concurrency control.
protected void IncrementVersion()
MarkEventsAsCommitted()
Marks all uncommitted domain events as committed by clearing them. This method should be called after successfully persisting events.
public void MarkEventsAsCommitted()
RemoveDomainEvent(IDomainEvent)
Removes a domain event from the aggregate.
protected void RemoveDomainEvent(IDomainEvent domainEvent)
Parameters
domainEventIDomainEventThe domain event to remove.
SetVersion(long)
Sets the version explicitly (used during rehydration from event store).
protected void SetVersion(long version)
Parameters
versionlongThe version to set.