Table of Contents

Class FakeRepository<TAggregate, TId>

Namespace
Trellis.Testing.Fakes
Assembly
Trellis.Testing.dll

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

TAggregate

The aggregate type.

TId

The 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

int

PublishedEvents

Gets the list of domain events published by saved aggregates.

public IReadOnlyList<IDomainEvent> PublishedEvents { get; }

Property Value

IReadOnlyList<IDomainEvent>

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

id TId

The aggregate ID.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<Result<Unit>>

A Result indicating success or NotFoundError.

Exists(TId)

Checks if an aggregate with the specified ID exists.

public bool Exists(TId id)

Parameters

id TId

The 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

predicate Func<TAggregate, bool>

The predicate to match.

Returns

Task<Maybe<TAggregate>>

Maybe with the first matching aggregate or None.

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

id TId

The aggregate ID.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<Maybe<TAggregate>>

Maybe with the aggregate or None.

Get(TId)

Gets an aggregate by ID without wrapping in Result.

public TAggregate? Get(TId id)

Parameters

id TId

The 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

id TId

The aggregate ID.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<Result<TAggregate>>

A Result containing the aggregate or a NotFoundError.

SaveAsync(TAggregate, CancellationToken)

Saves an aggregate and captures its domain events.

public Task<Result<Unit>> SaveAsync(TAggregate aggregate, CancellationToken cancellationToken = default)

Parameters

aggregate TAggregate

The aggregate to save.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<Result<Unit>>

A Result indicating success or failure.

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

predicate Func<TAggregate, bool>

The predicate to filter by.

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

specification Specification<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

propertySelector Func<TAggregate, object>

A function selecting the property to constrain.

Returns

FakeRepository<TAggregate, TId>

This repository for fluent chaining.

Examples

var repo = new FakeRepository<Customer, CustomerId>()
    .WithUniqueConstraint(c => c.Email);