Table of Contents

Class ServiceCollectionExtensions

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

Extension methods for the IServiceCollection interface.

public static class ServiceCollectionExtensions
Inheritance
ServiceCollectionExtensions

Examples

Registering DDD aggregate repositories in Savvy I/O involves AddAggregateRepository for aggregate persistence, AddRepository for read-model repositories, and AddUnitOfWork for coordinating saves. Each call takes the service interface, entity type, and key type as type arguments. The example registers all three patterns alongside a shared data source and resolves each service to verify the full domain persistence wiring.

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Cuemon.Threading;
using Microsoft.Extensions.DependencyInjection;
using Savvyio.Extensions.DependencyInjection;
using Savvyio;
using Savvyio.Domain;
using Savvyio.Extensions.DependencyInjection.Domain;

namespace ExampleApp;

public static class DomainRegistration
{
    public static IServiceCollection AddOrderPersistence(this IServiceCollection services)
    {
        services.AddDataSource<OrderDataSource>();
        services.AddUnitOfWork<OrderDataSource>();
        services.AddAggregateRepository<OrderRepository, OrderAggregate, Guid>();
        services.AddRepository<OrderRepository, OrderAggregate, Guid>();
        return services;
    }
}

public sealed class OrderDataSource : IDataSource, IUnitOfWork
{
    public Task SaveChangesAsync(Action<AsyncOptions> setup = null)
    {
        return Task.CompletedTask;
    }
}

public sealed class OrderAggregate : AggregateRoot<Guid>
{
    public OrderAggregate(Guid id, string customerId) : base(id)
    {
        CustomerId = customerId;
    }

    public string CustomerId { get; private set; }
}

public sealed class OrderRepository : IAggregateRepository<OrderAggregate, Guid>
{
    public OrderRepository(Savvyio.IDataSource dataSource)
    {
    }

    public void Add(OrderAggregate entity)
    {
    }

    public void AddRange(IEnumerable<OrderAggregate> entities)
    {
    }

    public Task<IEnumerable<OrderAggregate>> FindAllAsync(Expression<Func<OrderAggregate, bool>> predicate = null, Action<AsyncOptions> setup = null)
    {
        return Task.FromResult<IEnumerable<OrderAggregate>>(Array.Empty<OrderAggregate>());
    }

    public Task<OrderAggregate> FindAsync(Expression<Func<OrderAggregate, bool>> predicate, Action<AsyncOptions> setup = null)
    {
        return Task.FromResult(new OrderAggregate(Guid.NewGuid(), "ALFKI"));
    }

    public Task<OrderAggregate> GetByIdAsync(Guid id, Action<AsyncOptions> setup = null)
    {
        return Task.FromResult(new OrderAggregate(id, "ALFKI"));
    }

    public void Remove(OrderAggregate entity)
    {
    }

    public void RemoveRange(IEnumerable<OrderAggregate> entities)
    {
    }
}

Methods

AddAggregateRepository<TService, TEntity, TKey>(IServiceCollection, Action<ServiceOptions>)

Adds an implementation of IAggregateRepository<TEntity, TKey> to the specified IServiceCollection.

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

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

setup Action<ServiceOptions>

The ServiceOptions which may be configured.

Returns

IServiceCollection

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

Type Parameters

TService

The type of the IAggregateRepository<TEntity, TKey> to add.

TEntity

The type of the entity.

TKey

The type of the key that uniquely identifies the entity.

Remarks

If the underlying type of TService implements IDependencyInjectionMarker<TMarker> interface then this is automatically handled. Also, the implementation will be type forwarded accordingly.

Exceptions

ArgumentNullException

services cannot be null.

See Also
IAggregateRepository<TEntity, TKey>
IAggregateRepository<TEntity, TKey, TMarker>
IPersistentRepository<TEntity, TKey>
IPersistentRepository<TEntity, TKey, TMarker>

AddRepository<TService, TEntity, TKey>(IServiceCollection, Action<ServiceOptions>)

Adds an implementation of IPersistentRepository<TEntity, TKey> to the specified IServiceCollection.

public static IServiceCollection AddRepository<TService, TEntity, TKey>(this IServiceCollection services, Action<ServiceOptions> setup = null) where TService : class, IPersistentRepository<TEntity, TKey> where TEntity : class, IIdentity<TKey>

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

setup Action<ServiceOptions>

The ServiceOptions which may be configured.

Returns

IServiceCollection

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

Type Parameters

TService

The type of the IPersistentRepository<TEntity, TKey> to add.

TEntity

The type of the entity.

TKey

The type of the key that uniquely identifies the entity.

Remarks

If the underlying type of TService implements IDependencyInjectionMarker<TMarker> interface then this is automatically handled. Also, the implementation will be type forwarded accordingly.

See Also
IPersistentRepository<TEntity, TKey>
IPersistentRepository<TEntity, TKey, TMarker>
IWritableRepository<TEntity, TKey>
IWritableRepository<TEntity, TKey, TMarker>
IReadableRepository<TEntity, TKey>
IReadableRepository<TEntity, TKey, TMarker>
ISearchableRepository<TEntity, TKey>
ISearchableRepository<TEntity, TKey, TMarker>
IDeletableRepository<TEntity, TKey>
IDeletableRepository<TEntity, TKey, TMarker>

AddUnitOfWork<TService>(IServiceCollection, Action<ServiceOptions>)

Adds an implementation of IUnitOfWork to the specified IServiceCollection.

public static IServiceCollection AddUnitOfWork<TService>(this IServiceCollection services, Action<ServiceOptions> setup = null) where TService : class, IUnitOfWork

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

setup Action<ServiceOptions>

The ServiceOptions which may be configured.

Returns

IServiceCollection

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

Type Parameters

TService

The type of the IUnitOfWork to add.

Remarks

If the underlying type of TService implements IDependencyInjectionMarker<TMarker> interface then this is automatically handled. Also, the implementation will be type forwarded accordingly.

See Also
IUnitOfWork<TMarker>