Interface IEventSourcedRepository<TAggregate, TKey>
- Namespace
- Excalibur.EventSourcing
- Assembly
- Excalibur.EventSourcing.Abstractions.dll
Repository for loading and saving event-sourced aggregates with generic key support.
public interface IEventSourcedRepository<TAggregate, TKey> where TAggregate : class, IAggregateRoot<TKey>, IAggregateSnapshotSupport where TKey : notnull
Type Parameters
TAggregateThe aggregate type.
TKeyThe type of the aggregate identifier.
Examples
public class OrderRepository : IEventSourcedRepository<OrderAggregate, Guid>
{
// Implementation
}
// Usage
var order = await repository.GetByIdAsync(orderId, ct);
await repository.SaveAsync(order, order.ETag, ct);
Remarks
This interface provides CQRS write-side event sourcing repository operations:
- Load and save with strongly-typed keys
- ETag-based optimistic concurrency control
- Existence checks and soft delete via tombstone events
This interface follows CQRS best practice: it provides write-side operations only. Query operations belong in a separate read model or projection store.
Methods
DeleteAsync(TAggregate, CancellationToken)
Soft-deletes an aggregate by appending a tombstone event.
Task DeleteAsync(TAggregate aggregate, CancellationToken cancellationToken)
Parameters
aggregateTAggregateThe aggregate to delete.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task
A task that represents the asynchronous delete operation.
Remarks
This performs a soft delete by marking the aggregate as deleted. The aggregate's event history is preserved for audit purposes.
ExistsAsync(TKey, CancellationToken)
Checks if an aggregate exists.
Task<bool> ExistsAsync(TKey aggregateId, CancellationToken cancellationToken)
Parameters
aggregateIdTKeyThe aggregate identifier.
cancellationTokenCancellationTokenThe cancellation token.
Returns
GetByIdAsync(TKey, CancellationToken)
Gets an aggregate by its strongly-typed identifier.
Task<TAggregate?> GetByIdAsync(TKey aggregateId, CancellationToken cancellationToken)
Parameters
aggregateIdTKeyThe aggregate identifier.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task<TAggregate>
The aggregate if found; null if no stream exists for the identifier; or an aggregate in its initial state --
Version0, no history applied -- if the stream exists but has been erased. The erased case is deliberately distinct from both: it is not null, because the subject did exist, and it does not throw, because the erasure was lawful rather than a corruption. A caller that only null-checks will treat an erased stream as a live aggregate that happens to have no history.
SaveAsync(TAggregate, string?, CancellationToken)
Saves an aggregate with ETag-based optimistic concurrency check.
Task<SaveResult> SaveAsync(TAggregate aggregate, string? expectedETag, CancellationToken cancellationToken)
Parameters
aggregateTAggregateThe aggregate to save.
expectedETagstringThe expected ETag value. If the current ETag doesn't match, a ConcurrencyException is thrown. Pass null to skip ETag validation.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task<SaveResult>
A task that represents the asynchronous save operation.
Exceptions
- ConcurrencyException
Thrown when the expected ETag doesn't match the current aggregate ETag.
SaveAsync(TAggregate, CancellationToken)
Saves an aggregate and its uncommitted events.
Task<SaveResult> SaveAsync(TAggregate aggregate, CancellationToken cancellationToken)
Parameters
aggregateTAggregateThe aggregate to save.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task<SaveResult>
A task that represents the asynchronous save operation.