AddFactoryExtension 0.2.1
dotnet add package AddFactoryExtension --version 0.2.1
NuGet\Install-Package AddFactoryExtension -Version 0.2.1
<PackageReference Include="AddFactoryExtension" Version="0.2.1" />
<PackageVersion Include="AddFactoryExtension" Version="0.2.1" />
<PackageReference Include="AddFactoryExtension" />
paket add AddFactoryExtension --version 0.2.1
#r "nuget: AddFactoryExtension, 0.2.1"
#:package AddFactoryExtension@0.2.1
#addin nuget:?package=AddFactoryExtension&version=0.2.1
#tool nuget:?package=AddFactoryExtension&version=0.2.1
AddFactory extension method
This project simulates the Ninject's ToFactory functionality
but on IServiceCollection.
Example
Types.cs
public interface IBar
{
void DoSomething();
}
public class Bar : IBar
{
public void DoSomething()
=> Console.WriteLine("It works!!!");
}
// Let's add implementation of IBarFactory dynamically!
public interface IBarFactory
{
IBar Factory();
}
Program.cs
using Microsoft.Extensions.DependencyInjection;
public static class Program
{
static void Main()
{
ServiceCollection sc = new ServiceCollection();
sc.AddTransient<IBar, Bar>();
// dynamic factory service lifetime
// is singleton by default and can be changed
sc.AddFactory<IBarFactory>();
var sp = sc.BuildServiceProvider();
var barFactory = sp.GetRequiredService<IBarFactory>();
IBar bar = barFactory.Factory();
// outputs 'It works!!!!'
bar.DoSomething();
}
}
How it works
Magic. (System.Reflection.Emit)
Assembly scan for implemented interfaces
The AddFactory method scans for implementations of the
Factory method return types inside the assembly of
the factory interface type.
E.g. from the example above, the AddFactory will look
for implementations of IBar inside the assembly with
IBarFactory.
It is possible to change that behvaiour and specify which assemblies to scan.
sc.AddFactory<IBarFactory>(params Assembly[] assemblies)
Automatic injection of other services
AddFactory supports automatic injection of other services
inside the dynamically created factory and then feeding those
services to the newly factored services.
Note that the dependent services must come after any simple types (ints/longs/other num types, enums, structs, strings).
Example:
publi
public interface ISomeDependency { }
public interface IBar1 { }
public class Bar1 : IBar1
{
private int someParameter;
private ISomeDependency someDependency;
// ISomeDependency will be automatically fed by
// the dynamic factory
public Bar1(int someParameter, ISomeDependency someDependency)
{
this.someParameter = someParameter;
this.someDependency = someDependency;
}
public void DoSomething()
{
Console.WriteLine("It works!!!");
}
}
public interface IBar1Factory
{
// dynamic factory will inject ISomeDependency
// and then invoke the ctor with int
IBar1 Factory(int someParameter);
}
Limitations
- The project searches only for methods named
Factoryinside the factory interface. - The return type of the
Factorymethod must be an interface AddFactorywill throw an exception if there are multiple constructors matching the factory method signature.
| 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | 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.1
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
fix: services injection, more tests