Table of Contents

Class ServiceCollectionExtensions

Namespace
Savvyio.Extensions.DependencyInjection.Data
Assembly
Savvyio.Extensions.DependencyInjection.dll

Extension methods for the IServiceCollection interface.

public static class ServiceCollectionExtensions
Inheritance
ServiceCollectionExtensions

Examples

Register a custom data source and persistent data store with AddDataStore for application read and write models.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Cuemon.Threading;
using Microsoft.Extensions.DependencyInjection;
using Savvyio.Extensions.DependencyInjection;
using Savvyio;
using Savvyio.Data;
using Savvyio.Extensions.DependencyInjection.Data;

namespace ExampleApp;

public static class DataStoreRegistration
{
    public static IServiceCollection AddOrdersDataStore(this IServiceCollection services)
    {
        services.AddDataSource<InMemoryDataSource>();
        services.AddDataStore<OrderDataStore, OrderDocument>();
        return services;
    }
}

public sealed class InMemoryDataSource : Savvyio.IDataSource
{
}

public sealed class OrderDocument
{
    public string Id { get; init; } = string.Empty;
}

public sealed class OrderDataStore : IPersistentDataStore<OrderDocument, AsyncOptions>
{
    public OrderDataStore(Savvyio.IDataSource dataSource)
    {
    }

    public Task CreateAsync(OrderDocument dto, Action<AsyncOptions> setup = null)
    {
        return Task.CompletedTask;
    }

    public Task DeleteAsync(OrderDocument dto, Action<AsyncOptions> setup = null)
    {
        return Task.CompletedTask;
    }

    public Task<IEnumerable<OrderDocument>> FindAllAsync(Action<AsyncOptions> setup = null)
    {
        return Task.FromResult<IEnumerable<OrderDocument>>(Array.Empty<OrderDocument>());
    }

    public Task<OrderDocument> FindAsync(Action<AsyncOptions> setup = null)
    {
        return Task.FromResult(new OrderDocument());
    }

    public Task<OrderDocument> GetByIdAsync(object id, Action<AsyncOptions> setup = null)
    {
        return Task.FromResult(new OrderDocument { Id = id?.ToString() ?? string.Empty });
    }

    public Task UpdateAsync(OrderDocument dto, Action<AsyncOptions> setup = null)
    {
        return Task.CompletedTask;
    }
}

Methods

AddDataStore<TService, T>(IServiceCollection, Action<ServiceOptions>)

Adds an implementation of IPersistentDataStore<T, TOptions> to the specified IServiceCollection.

public static IServiceCollection AddDataStore<TService, T>(this IServiceCollection services, Action<ServiceOptions> setup = null) where TService : class, IPersistentDataStore<T, AsyncOptions> 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

TService

The type of the IPersistentDataStore<T, TOptions> to add.

T

The type of the DTO to use.

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
IPersistentDataStore<T, TOptions, TMarker>
IWritableDataStore<T, TMarker>
IReadableDataStore<T, TMarker>

AddDataStore<TService, T, TOptions>(IServiceCollection, Action<ServiceOptions>)

Adds an implementation of IPersistentDataStore<T, TOptions> to the specified IServiceCollection.

public static IServiceCollection AddDataStore<TService, T, TOptions>(this IServiceCollection services, Action<ServiceOptions> setup = null) where TService : class, IPersistentDataStore<T, TOptions> where T : class where TOptions : AsyncOptions, new()

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

TService

The type of the IPersistentDataStore<T, TOptions> to add.

T

The type of the DTO to use.

TOptions

The type of the options associated with T.

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
IPersistentDataStore<T, TOptions, TMarker>
IWritableDataStore<T, TMarker>
IReadableDataStore<T, TMarker>