Table of Contents

Class ServiceCollectionExtensions

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

Extension methods for the IServiceCollection interface.

public static class ServiceCollectionExtensions
Inheritance
ServiceCollectionExtensions

Examples

Register Savvy I/O in the DI container with AddSavvyIO, add the handler descriptor, a service locator, configured options, a data source, and a marshaller — all through IServiceCollection extension methods.

using System.IO;
using Microsoft.Extensions.DependencyInjection;
using Savvyio;
using Savvyio.Extensions.DependencyInjection;

namespace ExampleApp;

public static class ServiceRegistration
{
    public static IServiceCollection AddApplicationServices(this IServiceCollection services)
    {
        services.AddSavvyIO(options => options.EnableHandlerServicesDescriptor());
        services.AddHandlerServicesDescriptor();
        services.AddServiceLocator();
        services.AddConfiguredOptions<SavvyioOptions>(_ => { });
        services.AddDataSource<AppDataSource>();
        services.AddMarshaller<AppMarshaller>(p => new AppMarshaller());
        return services;
    }
}

public sealed class AppDataSource : IDataSource { }

public sealed class AppMarshaller : IMarshaller
{
    public Stream Serialize<TValue>(TValue value) => Stream.Null;
    public Stream Serialize(object value, System.Type inputType) => Stream.Null;
    public TValue Deserialize<TValue>(Stream data) => default!;
    public object Deserialize(Stream data, System.Type returnType) => null!;
}

Methods

AddConfiguredOptions<TOptions>(IServiceCollection, Action<TOptions>)

Registers the specified setup as a triple-configuration for both compatibility with (and outside the confines of) Microsoft Dependency Injection e.g., IOptions<TOptions>, Action<TOptions> and TOptions.

public static IServiceCollection AddConfiguredOptions<TOptions>(this IServiceCollection services, Action<TOptions> setup) where TOptions : class, IParameterObject, new()

Parameters

services IServiceCollection

The IServiceCollection to add the services to.

setup Action<TOptions>

The TOptions which need to be configured by the setup delegate.

Returns

IServiceCollection

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

Type Parameters

TOptions

The options type to be configured.

Exceptions

ArgumentNullException

services cannot be null -or- setup cannot be null.

ArgumentException

setup are not in a valid state.

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

Adds an implementation of IDataSource to the specified IServiceCollection.

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

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 IDataSource 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.

Exceptions

ArgumentNullException

services cannot be null.

See Also
IDataSource<TMarker>

AddHandlerServicesDescriptor(IServiceCollection)

public static IServiceCollection AddHandlerServicesDescriptor(this IServiceCollection services)

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

Returns

IServiceCollection

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

Exceptions

ArgumentNullException

services cannot be null.

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

Adds an implementation of IMarshaller to the specified IServiceCollection.

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

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 IMarshaller to add.

Exceptions

ArgumentNullException

services cannot be null.

AddMarshaller<TService>(IServiceCollection, Func<IServiceProvider, TService>, Action<ServiceOptions>)

Adds an implementation of IMarshaller to the specified IServiceCollection.

public static IServiceCollection AddMarshaller<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory, Action<ServiceOptions> setup = null) where TService : class, IMarshaller

Parameters

services IServiceCollection

The IServiceCollection to add the service to.

implementationFactory Func<IServiceProvider, TService>

The function delegate that creates the service.

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 IMarshaller to add.

Exceptions

ArgumentNullException

services cannot be null -or- implementationFactory cannot be null.

AddSavvyIO(IServiceCollection, Action<SavvyioDependencyInjectionOptions>)

Adds Savvy I/O related dispatcher- and handler- types to the specified IServiceCollection.

public static IServiceCollection AddSavvyIO(this IServiceCollection services, Action<SavvyioDependencyInjectionOptions> setup = null)

Parameters

services IServiceCollection

The IServiceCollection to extend.

setup Action<SavvyioDependencyInjectionOptions>

The SavvyioDependencyInjectionOptions which may be configured.

Returns

IServiceCollection

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

Exceptions

ArgumentNullException

services cannot be null.

AddServiceLocator(IServiceCollection, Action<ServiceLocatorOptions>)

Adds Savvy I/O service locator used to resolve necessary dependencies.

public static IServiceCollection AddServiceLocator(this IServiceCollection services, Action<ServiceLocatorOptions> setup = null)

Parameters

services IServiceCollection

The IServiceCollection to extend.

setup Action<ServiceLocatorOptions>

The ServiceLocatorOptions which may be configured.

Returns

IServiceCollection

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

Exceptions

ArgumentNullException

services cannot be null.