Table of Contents

Class ServiceCollectionExtensions

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

Extension methods for the IServiceCollection interface.

public static class ServiceCollectionExtensions
Inheritance
ServiceCollectionExtensions

Examples

Composing an EF Core data layer in Savvy I/O requires three registrations: AddEfCoreDataSource to bind the DbContext and IEfCoreDataSource, AddEfCoreDataStore for generic read/write stores, and AddEfCoreRepository for aggregate-scoped repositories. Each registration takes the concrete type as a type argument, and the data source must be registered first because stores and repositories depend on it. The example resolves both a repository and a data store from the built service provider to confirm the bindings are correct.

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Savvyio;
using Savvyio.Data;
using Savvyio.Domain;
using Savvyio.Extensions.DependencyInjection.EFCore;
using Savvyio.Extensions.EFCore;

namespace ExampleApp;

public sealed class RegistrationExample
{
    public ServiceProvider BuildProvider()
    {
        var services = new ServiceCollection();
        services.AddEfCoreDataSource(options =>
        {
            options.ContextConfigurator = builder => builder.EnableDetailedErrors();
            options.ModelConstructor = modelBuilder => modelBuilder.Entity<Product>().HasKey(product => product.Id);
        });
        services.AddEfCoreDataStore<Product>();
        services.AddEfCoreRepository<Product, Guid>();

        return services.BuildServiceProvider();
    }

    public bool HasExpectedRegistrations(ServiceProvider provider)
    {
        var dataSource = provider.GetRequiredService<IEfCoreDataSource>();
        var dataStore = provider.GetRequiredService<IPersistentDataStore<Product, EfCoreQueryOptions<Product>>>();
        var repository = provider.GetRequiredService<IPersistentRepository<Product, Guid>>();

        return dataSource is EfCoreDataSource &&
               dataStore is EfCoreDataStore<Product> &&
               repository is EfCoreRepository<Product, Guid>;
    }
}

public sealed class Product : IIdentity<Guid>
{
    public Product(Guid id, string name)
    {
        Id = id;
        Name = name;
    }

    public Guid Id { get; }

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

Methods

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

Adds an EfCoreDataSource implementation to the specified IServiceCollection.

public static IServiceCollection AddEfCoreDataSource(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 implementation will be type forwarded accordingly.

Exceptions

ArgumentNullException

services cannot be null.

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

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

public static IServiceCollection AddEfCoreDataSource<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<TMarker> 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

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

Remarks

The implementation will be type forwarded accordingly.

Exceptions

ArgumentNullException

services cannot be null.

AddEfCoreDataStore<T>(IServiceCollection, Action<ServiceOptions>)

Adds an implementation of EfCoreDataStore<T> to the specified IServiceCollection.

public static IServiceCollection AddEfCoreDataStore<T>(this IServiceCollection services, Action<ServiceOptions> setup = null) where T : class

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 calls can be chained.

Type Parameters

T

The type of the DTO.

Exceptions

ArgumentNullException

services cannot be null.

AddEfCoreDataStore<T, TMarker>(IServiceCollection, Action<ServiceOptions>)

Adds an implementation of EfCoreDataStore<T> to the specified IServiceCollection.

public static IServiceCollection AddEfCoreDataStore<T, TMarker>(this IServiceCollection services, Action<ServiceOptions> setup = null) where T : class

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 calls can be chained.

Type Parameters

T

The type of the DTO.

TMarker

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

Exceptions

ArgumentNullException

services cannot be null.

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

public static IServiceCollection AddEfCoreRepository<TEntity, TKey>(this IServiceCollection services, Action<ServiceOptions> setup = null) 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 calls can be chained.

Type Parameters

TEntity

The type of the entity.

TKey

The type of the key that uniquely identifies the entity.

Remarks

Exceptions

ArgumentNullException

services cannot be null.

See Also
EfCoreRepository<TEntity, TKey>

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

public static IServiceCollection AddEfCoreRepository<TEntity, TKey, TMarker>(this IServiceCollection services, Action<ServiceOptions> setup = null) 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 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

Exceptions

ArgumentNullException

services cannot be null.

See Also
EfCoreRepository<TEntity, TKey, TMarker>