Table of Contents

Class ServiceCollectionExtensions

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

Extension methods for the IServiceCollection interface.

public static class ServiceCollectionExtensions
Inheritance
ServiceCollectionExtensions

Examples

AddTracedAggregateRepository binds ITracedAggregateRepository<TAggregateRoot, TKey> to its implementation. Call it alongside AddDataSource and AddUnitOfWork to complete the event-sourcing persistence registration.

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

namespace ExampleApp;

public static class EventSourcingRegistration
{
    public static IServiceCollection Configure(IServiceCollection services)
    {
        services.AddDataSource<EventStoreSource>();
        services.AddUnitOfWork<EventStoreSource>();
        services.AddTracedAggregateRepository<OrderHistoryRepository, OrderHistory, Guid>();
        return services;
    }
}

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

public sealed class OrderHistory : ITracedAggregateRoot<Guid>
{
    public long Version => 0;
    public IReadOnlyList<ITracedDomainEvent> Events => Array.Empty<ITracedDomainEvent>();
    public void RemoveAllEvents() { }
    public IMetadataDictionary Metadata { get; } = new MetadataDictionary();
    public Guid Id { get; } = Guid.NewGuid();
}

public sealed class OrderHistoryRepository : ITracedAggregateRepository<OrderHistory, Guid>
{
    public OrderHistoryRepository(Savvyio.IDataSource source) { }
    public void Add(OrderHistory entity) { }
    public void AddRange(IEnumerable<OrderHistory> entities) { }
    public void Remove(OrderHistory entity) { }
    public void RemoveRange(IEnumerable<OrderHistory> entities) { }
    public Task<OrderHistory> FindAsync(Expression<Func<OrderHistory, bool>> predicate, Action<AsyncOptions> setup = null) => Task.FromResult(new OrderHistory());
    public Task<IEnumerable<OrderHistory>> FindAllAsync(Expression<Func<OrderHistory, bool>> predicate = null, Action<AsyncOptions> setup = null) => Task.FromResult<IEnumerable<OrderHistory>>(Array.Empty<OrderHistory>());
    public Task<OrderHistory> GetByIdAsync(Guid id, Action<AsyncOptions> setup = null) => Task.FromResult(new OrderHistory());
}

Methods

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

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

public static IServiceCollection AddTracedAggregateRepository<TService, TEntity, TKey>(this IServiceCollection services, Action<ServiceOptions> setup = null) where TService : class, ITracedAggregateRepository<TEntity, TKey> where TEntity : class, ITracedAggregateRoot<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 ITracedAggregateRepository<TEntity, TKey> interface 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
ITracedAggregateRepository<TEntity, TKey, TMarker>
IWritableRepository<TEntity, TKey>
IWritableRepository<TEntity, TKey, TMarker>
IReadableRepository<TEntity, TKey>
IReadableRepository<TEntity, TKey, TMarker>