Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Latest commit

 

History

History
57 lines (40 loc) · 1.76 KB

File metadata and controls

57 lines (40 loc) · 1.76 KB
sidebar_position 3
sidebar_label Dependency Injection

⚠️ Deprecation Warning ⚠️

This library has been deprecated and will no longer receive updates.


Dependency Injection

To register an implementation of ICrypto with the Microsoft.Extensions.DependencyInjection container, the RockLib.Encryption package provides a AddCrypto extension method with several overloads. Each of the methods registers both the ICrypto and IAsyncCrypto interfaces.

Implemenations of the ICrypto interface should also define dependency injection extension methods specific to the implemenation. See SymmetricCrypto for an example.


The overload with no additional parameters registers (as singleton) the ICrypto implementation that is returned by the Crypto.Current static property. By default, this instance is defined in configuration.

public void ConfigureServices(IServiceCollection services)
{
    services.AddCrypto();
}

The overload with an ICrypto parameter registers the specified instance as singleton.

public void ConfigureServices(IServiceCollection services)
{
    ICrypto crypto = // TODO: instantiate an ICrypto implementation.

    services.AddCrypto(crypto);
}

The overload with a Func<IServiceProvider, ICrypto> parameter registers the ICrypto returned by the cryptoFactory function as singleton.

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IMyDependency, MyConcreteDependency>();

    services.AddCrypto(serviceProvider =>
    {
        IMyDependency myDependency = serviceProvider.GetRequiredService<IMyDependency>();
        return new MyCrypto(myDependency);
    });
}