Class FakeRepository<TAggregate, TId>
In-memory fake repository for testing aggregates. Provides a simple in-memory store with domain event tracking.
public class FakeRepository<TAggregate, TId> where TAggregate : Aggregate<TId> where TId : notnull
Type Parameters
TAggregateThe aggregate type.
TIdThe aggregate ID type.
- Inheritance
-
FakeRepository<TAggregate, TId>
- Inherited Members
- Extension Methods
Properties
Count
Gets the count of stored aggregates.
public int Count { get; }
Property Value
PublishedEvents
Gets the list of domain events published by saved aggregates.
public IReadOnlyList<IDomainEvent> PublishedEvents { get; }
Property Value
Methods
Clear()
Clears all stored aggregates and published events.
public void Clear()
DeleteAsync(TId, CancellationToken)
Deletes an aggregate by its ID.
public Task<Result<Unit>> DeleteAsync(TId id, CancellationToken cancellationToken = default)
Parameters
idTIdThe aggregate ID.
cancellationTokenCancellationTokenCancellation token.
Returns
Exists(TId)
Checks if an aggregate with the specified ID exists.
public bool Exists(TId id)
Parameters
idTIdThe aggregate ID.
Returns
- bool
True if the aggregate exists, false otherwise.
FindAsync(Func<TAggregate, bool>)
Finds the first aggregate matching the predicate, returning None if no match.
Use in test repository adapters for custom query methods (e.g., FindByEmailAsync).
public Task<Maybe<TAggregate>> FindAsync(Func<TAggregate, bool> predicate)
Parameters
Returns
FindByIdAsync(TId, CancellationToken)
Finds an aggregate by its ID, returning Maybe if not found.
public Task<Maybe<TAggregate>> FindByIdAsync(TId id, CancellationToken cancellationToken = default)
Parameters
idTIdThe aggregate ID.
cancellationTokenCancellationTokenCancellation token.
Returns
Get(TId)
Gets an aggregate by ID without wrapping in Result.
public TAggregate? Get(TId id)
Parameters
idTIdThe aggregate ID.
Returns
- TAggregate
The aggregate or null if not found.
GetAll()
Gets all stored aggregates.
public IEnumerable<TAggregate> GetAll()
Returns
- IEnumerable<TAggregate>
All aggregates in the repository.
GetByIdAsync(TId, CancellationToken)
Gets an aggregate by its ID.
public Task<Result<TAggregate>> GetByIdAsync(TId id, CancellationToken cancellationToken = default)
Parameters
idTIdThe aggregate ID.
cancellationTokenCancellationTokenCancellation token.
Returns
SaveAsync(TAggregate, CancellationToken)
Saves an aggregate and captures its domain events.
public Task<Result<Unit>> SaveAsync(TAggregate aggregate, CancellationToken cancellationToken = default)
Parameters
aggregateTAggregateThe aggregate to save.
cancellationTokenCancellationTokenCancellation token.
Returns
WhereAsync(Func<TAggregate, bool>)
Returns all aggregates matching the predicate.
Use in test repository adapters for custom query methods (e.g., GetByCustomerIdAsync).
public Task<IReadOnlyList<TAggregate>> WhereAsync(Func<TAggregate, bool> predicate)
Parameters
Returns
- Task<IReadOnlyList<TAggregate>>
A list of matching aggregates.
WhereAsync(Specification<TAggregate>)
Returns all aggregates matching the specification.
Use in test repository adapters for specification-based queries (e.g., GetOverdueOrdersAsync).
public Task<IReadOnlyList<TAggregate>> WhereAsync(Specification<TAggregate> specification)
Parameters
specificationSpecification<TAggregate>The specification to evaluate.
Returns
- Task<IReadOnlyList<TAggregate>>
A list of matching aggregates.
WithUniqueConstraint(Func<TAggregate, object?>)
Adds a unique constraint on the specified property. When SaveAsync(TAggregate, CancellationToken) is called, the repository checks that no other aggregate (with a different ID) has the same value for this property. Returns a ConflictError on violation.
public FakeRepository<TAggregate, TId> WithUniqueConstraint(Func<TAggregate, object?> propertySelector)
Parameters
Returns
- FakeRepository<TAggregate, TId>
This repository for fluent chaining.
Examples
var repo = new FakeRepository<Customer, CustomerId>()
.WithUniqueConstraint(c => c.Email);