Table of Contents

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

TId

The type of the aggregate root identifier.

Inheritance
Entity<TId>
AggregateRoot<TId>
Implements
Inherited Members

Constructors

AggregateRoot(TId, ILockingStrategy?)

Initializes a new instance of the AggregateRoot<TId> class.

protected AggregateRoot(TId id, ILockingStrategy? lockingStrategy = null)

Parameters

id TId

The aggregate root identifier.

lockingStrategy ILockingStrategy

The 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

IReadOnlyCollection<IDomainEvent>

HasDomainEvents

Gets a value indicating whether there are uncommitted domain events.

public bool HasDomainEvents { get; }

Property Value

bool

Version

Gets the version for optimistic concurrency control.

public long Version { get; }

Property Value

long

Methods

AddDomainEvent(IDomainEvent)

Adds a domain event to the aggregate. Prevents duplicate events using content-based deduplication.

protected void AddDomainEvent(IDomainEvent domainEvent)

Parameters

domainEvent IDomainEvent

The 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

disposing bool

True 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

domainEvent IDomainEvent

The domain event to remove.

SetVersion(long)

Sets the version explicitly (used during rehydration from event store).

protected void SetVersion(long version)

Parameters

version long

The version to set.