Table of Contents

Class EfCoreAggregateRepository<TEntity, TKey>

Namespace
Savvyio.Extensions.EFCore.Domain
Assembly
Savvyio.Extensions.EFCore.Domain.dll

Provides an implementation of the EfCoreRepository<TEntity, TKey> that is optimized for Domain Driven Design.

public class EfCoreAggregateRepository<TEntity, TKey> : EfCoreRepository<TEntity, TKey>, IAggregateRepository<TEntity, TKey>, IPersistentRepository<TEntity, TKey>, IWritableRepository<TEntity, TKey>, IReadableRepository<TEntity, TKey>, ISearchableRepository<TEntity, TKey>, IDeletableRepository<TEntity, TKey>, IRepository<TEntity, TKey> where TEntity : class, IAggregateRoot<IDomainEvent, TKey>

Type Parameters

TEntity

The type of the entity that implements the IAggregateRoot<TEvent> interface.

TKey

The type of the key that uniquely identifies the entity.

Inheritance
EfCoreRepository<TEntity, TKey>
EfCoreAggregateRepository<TEntity, TKey>
Implements
IAggregateRepository<TEntity, TKey>
IPersistentRepository<TEntity, TKey>
IWritableRepository<TEntity, TKey>
IReadableRepository<TEntity, TKey>
ISearchableRepository<TEntity, TKey>
IDeletableRepository<TEntity, TKey>
IRepository<TEntity, TKey>
Inherited Members

Examples

EfCoreAggregateRepository<TAggregateRoot, TKey, TContext> provides EF Core persistence for aggregate roots with strict boundary enforcement. Subclass it by providing the aggregate root type and key type, then inject the aggregate data source. The example creates a concrete repository, calls AddAsync, and resolves the aggregate to verify the persistence workflow.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Savvyio.Domain;
using Savvyio.Extensions.EFCore;
using Savvyio.Extensions.EFCore.Domain;

namespace ExampleApp;

public sealed class OrderingQueries
{
    public Task<IEnumerable<OrderAggregate>> LoadPriorityOrdersAsync()
    {
        var source = new OrderingDataSource(new EfCoreDataSourceOptions());
        var repository = new OrderingRepository(source);

        repository.Add(OrderAggregate.Place(Guid.NewGuid(), "PO-4096", true));
        return repository.FindPriorityOrdersAsync();
    }
}

public sealed class OrderingDataSource : EfCoreDataSource
{
    public OrderingDataSource(EfCoreDataSourceOptions options) : base(options)
    {
    }
}

public sealed class OrderingRepository : EfCoreAggregateRepository<OrderAggregate, Guid>
{
    public OrderingRepository(IEfCoreDataSource source) : base(source)
    {
    }

    public Task<IEnumerable<OrderAggregate>> FindPriorityOrdersAsync()
    {
        return FindAllAsync(order => order.IsPriority);
    }
}

public sealed class OrderAggregate : Aggregate<Guid, IDomainEvent>, IAggregateRoot<IDomainEvent, Guid>
{
    private OrderAggregate()
    {
    }

    private OrderAggregate(Guid id, string orderNumber, bool isPriority) : base(id)
    {
        OrderNumber = orderNumber;
        IsPriority = isPriority;
        AddEvent(new OrderPlaced(orderNumber));
    }

    public string OrderNumber { get; private set; } = string.Empty;

    public bool IsPriority { get; private set; }

    public static OrderAggregate Place(Guid id, string orderNumber, bool isPriority)
    {
        return new OrderAggregate(id, orderNumber, isPriority);
    }
}

public sealed record OrderPlaced(string OrderNumber) : DomainEvent;

Constructors

EfCoreAggregateRepository(IEfCoreDataSource)

Initializes a new instance of the EfCoreRepository<TEntity, TKey> class.

public EfCoreAggregateRepository(IEfCoreDataSource source)

Parameters

source IEfCoreDataSource

The IEfCoreDataSource that handles actual I/O communication with a source of data.

See Also

EfCoreRepository<TEntity, TKey>