Table of Contents

Class ServiceCollectionExtensions

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

Extension methods for the IServiceCollection interface.

public static class ServiceCollectionExtensions
Inheritance
ServiceCollectionExtensions

Examples

Registering domain aggregates backed by EF Core requires two registrations: AddEfCoreAggregateDataSource to bind the aggregate context as IEfCoreDataSource, and AddEfCoreAggregateRepository to add the repository on top. The aggregate data source sets the boundary for which context the repository uses; it must be registered first. The example registers both services and resolves the repository interface to confirm the aggregate wiring.

using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Savvyio.Domain;
using Savvyio.Extensions.DependencyInjection.Domain;
using Savvyio.Extensions.DependencyInjection.EFCore;
using Savvyio.Extensions.DependencyInjection.EFCore.Domain;

namespace ExampleApp;

public sealed class AggregateRegistrationExample
{
    public ServiceProvider BuildProvider()
    {
        var services = new ServiceCollection();
        services.AddSingleton<IDomainEventDispatcher, RecordingDomainEventDispatcher>();
        services.AddEfCoreAggregateDataSource(options =>
        {
            options.ContextConfigurator = builder => builder.EnableDetailedErrors();
            options.ModelConstructor = modelBuilder => modelBuilder.Entity<OrderAggregate>().HasKey(order => order.Id);
        });
        services.AddEfCoreAggregateRepository<OrderAggregate, Guid>();

        return services.BuildServiceProvider();
    }

    public bool HasExpectedRegistrations(ServiceProvider provider)
    {
        var repository = provider.GetRequiredService<IAggregateRepository<OrderAggregate, Guid>>();

        return repository is Savvyio.Extensions.EFCore.Domain.EfCoreAggregateRepository<OrderAggregate, Guid>;
    }
}

public sealed class RecordingDomainEventDispatcher : IDomainEventDispatcher
{
    public void Raise(IDomainEvent request)
    {
    }

    public Task RaiseAsync(IDomainEvent request, Action<Cuemon.Threading.AsyncOptions>? setup = null)
    {
        return Task.CompletedTask;
    }
}

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

Methods

AddEfCoreAggregateDataSource(IServiceCollection, Action<EfCoreDataSourceOptions>, Action<ServiceOptions>)

Adds an EfCoreDataSource implementation to the specified IServiceCollection.

public static IServiceCollection AddEfCoreAggregateDataSource(this IServiceCollection services, Action<EfCoreDataSourceOptions> dataSourceSetup, Action<ServiceOptions> serviceSetup = null)

Parameters

services IServiceCollection

The IServiceCollection to extend.

dataSourceSetup Action<EfCoreDataSourceOptions>

The EfCoreDataSourceOptions which need to be configured.

serviceSetup Action<ServiceOptions>

The ServiceOptions which may be configured.

Returns

IServiceCollection

A reference to services so that additional calls can be chained.

Remarks

The EfCoreAggregateDataSource will be type forwarded accordingly.

Exceptions

ArgumentNullException

services cannot be null.

AddEfCoreAggregateDataSource<TMarker>(IServiceCollection, Action<EfCoreDataSourceOptions<TMarker>>, Action<ServiceOptions>)

Adds an EfCoreDataSource<TMarker> implementation to the specified IServiceCollection.

public static IServiceCollection AddEfCoreAggregateDataSource<TMarker>(this IServiceCollection services, Action<EfCoreDataSourceOptions<TMarker>> dataSourceSetup, Action<ServiceOptions> serviceSetup = null)

Parameters

services IServiceCollection

The IServiceCollection to extend.

dataSourceSetup Action<EfCoreDataSourceOptions<TMarker>>

The EfCoreDataSourceOptions which need to be configured.

serviceSetup Action<ServiceOptions>

The ServiceOptions which may be configured.

Returns

IServiceCollection

A reference to services so that additional calls can be chained.

Type Parameters

TMarker

Remarks

The EfCoreAggregateDataSource<TMarker> will be type forwarded accordingly.

Exceptions

ArgumentNullException

services cannot be null.

AddEfCoreAggregateRepository<TEntity, TKey>(IServiceCollection, Action<ServiceOptions>)

public static IServiceCollection AddEfCoreAggregateRepository<TEntity, TKey>(this IServiceCollection services, Action<ServiceOptions> setup = null) where TEntity : class, IAggregateRoot<IDomainEvent, TKey>

Parameters

services IServiceCollection

The IServiceCollection to extend.

setup Action<ServiceOptions>

The ServiceOptions which may be configured.

Returns

IServiceCollection

A reference to services so that additional calls can be chained.

Type Parameters

TEntity

The type of the entity.

TKey

The type of the key that uniquely identifies the entity.

Remarks

The EfCoreAggregateRepository<TEntity, TKey, TMarker> will be type forwarded accordingly.

Exceptions

ArgumentNullException

services cannot be null.

See Also

AddEfCoreAggregateRepository<TEntity, TKey, TMarker>(IServiceCollection, Action<ServiceOptions>)

public static IServiceCollection AddEfCoreAggregateRepository<TEntity, TKey, TMarker>(this IServiceCollection services, Action<ServiceOptions> setup = null) where TEntity : class, IAggregateRoot<IDomainEvent, TKey>

Parameters

services IServiceCollection

The IServiceCollection to extend.

setup Action<ServiceOptions>

The ServiceOptions which may be configured.

Returns

IServiceCollection

A reference to services so that additional calls can be chained.

Type Parameters

TEntity

The type of the entity.

TKey

The type of the key that uniquely identifies the entity.

TMarker

The type used to mark the implementation that this repository represents. Optimized for Microsoft Dependency Injection.

Remarks

The EfCoreAggregateRepository<TEntity, TKey, TMarker> will be type forwarded accordingly.

Exceptions

ArgumentNullException

services cannot be null.

See Also
EfCoreAggregateRepository<TEntity, TKey, TMarker>