DependencyInjection.GenericFactory 1.0.1

.NET Standard 2.0
Install-Package DependencyInjection.GenericFactory -Version 1.0.1
dotnet add package DependencyInjection.GenericFactory --version 1.0.1
<PackageReference Include="DependencyInjection.GenericFactory" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DependencyInjection.GenericFactory --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DependencyInjection.GenericFactory, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install DependencyInjection.GenericFactory as a Cake Addin
#addin nuget:?package=DependencyInjection.GenericFactory&version=1.0.1

// Install DependencyInjection.GenericFactory as a Cake Tool
#tool nuget:?package=DependencyInjection.GenericFactory&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

DependencyInjection.GenericFactory GitHub Nuget

This library is an extension for Microsoft.Extensions.DependencyInjection that allows easy registration and use of DI factories that allow for runtime dependency resolution while retaining strong typing.

Usage

The central piece is the IFactory generic interface, which has many versions with different counts of generic parameters. For example, for two runtime resolved parameters you would use IFactory<TService, TArg1, TArg2>.

Factories can be registered using IServiceCollection.AddFactory<TService, TImplementation, TArg1, TArg2>(), or if you need specific control on how the service is being created, you can register a factory delegate via IServiceCollection.AddFactory<TService. TArg1, TArg2>(Func<IServiceProvider, TArg1, TArg2, TService>). Both methods will register a factory which will be registered to the ServiceCollection via the IFactory interface.

Factories can then be resolved by regular dependancy injection methods, or by calling IServiceProvider.GetFactory<TService, Arg1, Arg2>(). A GetRequiredFactory version is also available.

When to use IFactory<T> instead of IServiceProvider?

The rule of thumb should be to always prefer using IServiceProvider, unless:

  • Your service has dependencies that cannot be known during service registration
  • You want to be explicit that I will only create instances of this specific type

Example:

class Startup
{
    public IServiceCollection RegisterServices(IServiceCollection services)
    {
        services.AddFactory<IFooService, FooService, UnregisteredDependency>();
    }

    // ...
}

interface IFooService {}

class FooService : IFooService
{
    public FooService(UnregisteredDependency dependency) { }
}

class OtherService
{
    private IFactory<IFooService, IRuntimeDependency> _factory;

    public OtherService(IFactory<IFooService, IRuntimeDependency> factory)
    {
        _factory = factory;
    }

    public void Run(IRuntimeDependency runtimeDependency)
    {
        var fooService = factory.CreateService(runtimeDependency);
        // use fooService
    }
}

Why not ActivatorUtilities.CreateInstance?

  • It's a static class that can't be injected, and can't be mocked
  • It's comparatively slow
  • It's weakly typed

In fact, the library internally uses a WeaklyTypedFactory<TService>, which is basically a caching wrapper around ActivatorUtilities.CreateFactory that gets registered as a singleton, and solves the first two problems. The GenericFactory is then a wrapper around that, that solves the last problem sort of.

Sidenote: The weakly typed factory can be registered by calling IServiceCollection.AddWeaklyTypedFactory<TService,TImplementation>() and is then available via IWeaklyTypedFactory<TService>, but using it directly is not the recommended approach.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
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
1.0.1 3,634 3/6/2020
1.0.0 295 3/5/2020