NamedResolver 2.2.0

dotnet add package NamedResolver --version 2.2.0                
NuGet\Install-Package NamedResolver -Version 2.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NamedResolver" Version="2.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NamedResolver --version 2.2.0                
#r "nuget: NamedResolver, 2.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NamedResolver as a Cake Addin
#addin nuget:?package=NamedResolver&version=2.2.0

// Install NamedResolver as a Cake Tool
#tool nuget:?package=NamedResolver&version=2.2.0                

NamedResolver

An abstraction that provide ability to use interface with multiple implementations or preconfigured instances in easy way. Drop in replacement for simple switch case factories.

UnitTest Coverage Status

Look at tests for additional examples.

Use cases

let's assume we have this:

public interface ISomeInterface {}

public class FirstImplementation : ISomeInterface {}

public class SecondImplementation : ISomeInterface {}

And register in dependency injection container like this:


services.AddScoped<ISomeInterface, FirstImplementation>();
services.AddScoped<ISomeInterface, SecondImplementation>();

And sometimes we need to resolve one of implementation of interface directly.

In most cases, for resolve, we inject IEnumerable<TInterface> in our DependentClass and then search using typeof(T).

public class DependentClass
{
    private readonly ISomeInterface _firstImplementation;

    public DependentClass(IEnumerable<ISomeInterface> implementations)
    {
        // in this case we always resolve from DI all implementations of ISomeInterface.
        _firstImplementation =
            implementations.SingleOrDefault(i => i.GetType() == typeof(FirstImplementation)) ??
            throw new ArgumentNullException($"Cannot resolve instance of type {typeof(FirstImplementation).FullName}");
    }
}

Also we can register to IServiceCollection our implementation without interface, and inject implementation into DependenentClass explicitly, but it is not good for unit testing:


services.AddScoped<FirstImplementation>();
services.AddScoped<SecondImplementation>();

public class DependentClass
{
    private readonly ISomeInterface _firstImplementation;

    public DependentClass(FirstImplementation firstImplementation)
    {
        _firstImplementation = firstImplementation;
    }
}

Same with NamedResolver


services.AddNamed<string, ISomeInterface>(ServiceLifeTime.Scoped)
        .Add<FirstImplementation>("First");
        .Add<SecondImplementation>("Second");

public class DependentClass
{
    private readonly ISomeInterface _firstImplementation;

    public DependentClass(INamedResolver<string, ISomeInterface> resolver)
    {
        _firstImplementation = resolver.Get("First");
    }
}

Features:

Default instance:

public class DefaultImplementation : ISomeInterface {}

services.AddNamed<string, ISomeInterface>(ServiceLifeTime.Scoped)
        .Add<FirstImplementation>("First");
        .Add<SecondImplementation>("Second")
        .Add<DefaultImplementation>(); // default - without name parameter


public class DependentClass
{
    private readonly ISomeInterface _defaultImplementation;

    public DependentClass(ISomeInterface someInterface)
    {
        // DefaultImplementation would be injected
        _defaultImplementation = someInterface;
    }
}

Preconfigured instances


services.AddNamed<string, ISomeInterface>(ServiceLifeTime.Scoped)
        // note that such factory registration will not respect lifetime scope, and will be created every time!
        .Add<FirstImplementation>("FirstUseCase", _ => new FirstImplementation("FirstUseCase"));
        .Add<FirstImplementation>("SecondUseCase", _ => new FirstImplementation("SecondUseCase"));

Enum or custom class as discriminator


public enum MyEnum
{
    Default = 0,
    FirstUseCase = 1,
    SecondUseCase = 2
}

services.AddNamed<MyEnum, ISomeInterface>()
        .Add<FirstImplementation>(MyEnum.FirstUseCase)
        .Add<SecondImplementation>(MyEnum.SecondUseCase)
        .Add<DefaultImplementation>(); // or MyEnum.Default

for correct search type by class you also should implement IEquatable<T> or IEqualityComparer<T> and provide it to AddNamed method. By default used EqualityComparer<T>.Default

Resolve all


public class DependentClass
{
    private readonly IEnumerable<ISomeInterface> _implementations;
    private readonly IEnumerable<ISomeInterface> _fromSampleNamespace;
    private readonly IEnumerable<(string name, ISomeInterface instance)> _implementationsWithNames;

    public DependentClass(INamedResolver<string, ISomeInterface> resolver)
    {
        _implementations = resolver.GetAll();
        _implementationsWithNames = resolver.GetAllWithNames();
        _fromSampleNamespace = resolver.GetAll(t => t.Namespace.StartsWith("Sample"));
    }
}

or with IReadOnlyList<T>. With injecting IEnumerable<T> you get only default implementation or InvalidOperationException.


public class DependentClass
{
    private readonly IReadOnlyList<ISomeInterface> _implementations;

    // same result as INamedResolver<string, ISomeInterface>.GetAll method
    public DependentClass(IReadOnlyList<ISomeInterface> implementations)
    {
        _implementations = implementations;
    }
}

Resolve with delegate


public delegate TInterface ResolveNamed<in TDiscriminator, out TInterface>(
    TDiscriminator name = default
)

public class SomeClass
{
    private readonly ISomeInterface _implementation;

    public SomeClass(ResolveNamed<string, ISomeInterface> resolveNamedFunc)
    {
        _implementation = resolveNamedFunc("Test");
	}
}

Safe TryAdd both generic/non-generic method


services.AddNamed<string, ISomeInterface>(ServiceLifeTime.Scoped)
        .Add<FirstImplementation>("FirstUseCase", _ => new FirstImplementation("FirstUseCase"));
        // instance with name "FirstUseCase" already registered above,
        // this TryAdd with same name has no effect.
        .TryAdd<FirstImplementation>("FirstUseCase", _ => new FirstImplementation("FirstUseCase_skipped"));

TryAdd use case

this method would be usefull for other libraries. Sometimes we want to register some default implementation after user configure callback call, without exceptions or unwanted replaces with unexpected behaviour:


#region library code

public class SomeLibraryOptions
{
    public INamedRegistratorBuilder<string, ISomeInterface> SomeInterfaceRegistrator { get; }

    public SomeLibraryOptions(INamedRegistratorBuilder<string, ISomeInterface> registratorBuilder)
    {
        SomeInterfaceRegistrator = registratorBuilder;
    }
}

public static class SomeLibraryServiceCollectionExtensions
{
    public static IServiceCollection AddSomeLibrary(this IServiceCollection services, Action<SomeLibraryOptions> configure = null)
    {
        // take a builder reference
        var builder = services.AddNamed<string, ISomeInterface>();

        // init library options with builder
        var options = new SomeLibraryOptions(builder);

        // let call configure options callback with user code if it not null
        configure?.Invoke(options);

        // try to add default implementation, if user not configured it.
        builder.TryAdd<DefaultImplementation>();

        return services;
    }
}

#endregion library code

#region user code

services.AddSomeLibrary((options) =>
{
    options.SomeInterfaceRegistrator
           .Add<UserCustomImplementation>(); // registered as default.
});

#endregion user code

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.0 19,533 4/3/2021
2.1.0 10,355 5/19/2020
2.0.0 454 4/30/2020
1.2.0 1,904 2/3/2020
1.1.0 500 1/27/2020
1.0.0 534 1/24/2020