DependencyInjection.GenericFactory
1.0.1
dotnet add package DependencyInjection.GenericFactory --version 1.0.1
NuGet\Install-Package DependencyInjection.GenericFactory -Version 1.0.1
<PackageReference Include="DependencyInjection.GenericFactory" Version="1.0.1" />
paket add DependencyInjection.GenericFactory --version 1.0.1
#r "nuget: DependencyInjection.GenericFactory, 1.0.1"
// 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
DependencyInjection.GenericFactory
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 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. |
-
.NETStandard 2.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.