Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/czmirek/addfactoryextension
This project simulates the Ninject's ToFactory functionality but with IServiceCollection.
https://github.com/czmirek/addfactoryextension
csharp dotnet-core factory ioc
Last synced: 3 days ago
JSON representation
This project simulates the Ninject's ToFactory functionality but with IServiceCollection.
- Host: GitHub
- URL: https://github.com/czmirek/addfactoryextension
- Owner: czmirek
- License: mit
- Created: 2020-10-14T10:41:56.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2020-10-23T18:22:32.000Z (over 4 years ago)
- Last Synced: 2024-12-21T05:28:10.890Z (about 1 month ago)
- Topics: csharp, dotnet-core, factory, ioc
- Language: C#
- Homepage: https://github.com/czmirek/IServiceCollection.AddFactory-extension
- Size: 41 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AddFactory extension method
This project simulates the Ninject's [`ToFactory`](https://github.com/ninject/Ninject.Extensions.Factory/wiki/Factory-interface) functionality
but on `IServiceCollection`.[![NuGet](http://img.shields.io/nuget/v/AddFactoryExtension.svg)](https://www.nuget.org/packages/AddFactoryExtension/)
## Example
Types.cs
```csharp
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
```csharp
using Microsoft.Extensions.DependencyInjection;public static class Program
{
static void Main()
{
ServiceCollection sc = new ServiceCollection();
sc.AddTransient();// dynamic factory service lifetime
// is singleton by default and can be changed
sc.AddFactory();var sp = sc.BuildServiceProvider();
var barFactory = sp.GetRequiredService();
IBar bar = barFactory.Factory();
// outputs 'It works!!!!'
bar.DoSomething();
}
}
```
## How it worksMagic. (`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.```csharp
sc.AddFactory(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:
```csharp
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 `Factory` inside the factory interface.
- The return type of the `Factory` method must be an interface
- `AddFactory` will throw an exception if there are multiple constructors matching the factory method signature.