Skip to main content

FactoryGenerator.Abstractions by Ivan Mazurenko

NuGet / site data

NugetNuget GitHub last commit GitHub Repo stars

Details

Info

Author

note

Ivan Mazurenko Alt text

Original Readme

note

FactoryGenerator

.NET source generator that automatically generates and registers factories

Installation

Add the following packages to your project:

$ dotnet add package FactoryGenerator.Abstractions
$ dotnet add package FactoryGenerator.Microsoft.Extensions.DependencyInjection

Usage

Add the attribute to your class

Use one of the GenerateIFactory attributes to specify how your factory should be generated:

[GenerateIFactory<int>]
public class Service(int value, Dependency dependency)
{
// ...
}

This will generate an implementation of IFactory<int, Service>, allowing you to create instances of Service with an int parameter while automatically resolving other dependencies from the DI container. FactoryGenerator provides multiple attribute variations depending on the number of parameters your factory should accept.

Register generated factories in the DI container

The RegisterGeneratedFactories() method automatically registers all factories created by the source generator.


var serviceCollection = new ServiceCollection()
.RegisterGeneratedFactories();

Use the factory

using var serviceProvider = serviceCollection.BuildServiceProvider();

var factory = serviceProvider.GetRequiredService<IFactory<int, Service>>();

var service = factory.Create(1);

Full sample can be found here.

About

note

Generating DI Factory based on the first parameter(s)

How to use

Example (source csproj, source files)

This is the CSharp Project that references FactoryGenerator.Abstractions

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FactoryGenerator.Abstractions" Version="0.0.23" />
<PackageReference Include="FactoryGenerator.Microsoft.Extensions.DependencyInjection" Version="0.0.22" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.8" />
</ItemGroup>

</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

// <auto-generated/>
using FactoryGenerator.Abstractions;
using Microsoft.Extensions.DependencyInjection;

namespace AutoGeneratedNamespaceFromFactoryGenerator;

public static class ServiceCollectionExtensions
{
public static IServiceCollection RegisterGeneratedFactories(this IServiceCollection self)
{
self.AddTransient<FactoryGenerator.Abstractions.IFactory<global::InjectDemo.PersonType, InjectDemo.PersonFactory>, InjectDemo.PersonTypePersonFactoryFactory>();

return self;
}
}

Useful

Download Example (.NET C#)

Share FactoryGenerator.Abstractions

https://ignatandrei.github.io/RSCG_Examples/v2/docs/FactoryGenerator.Abstractions

Category "DependencyInjection" has the following generators:

1 AutoRegisterInject

2 BunnyTailServiceRegistration

3 DependencyModules.SourceGenerator

4 depso

5 FactoryGenerator

6 FactoryGenerator.Abstractions

7 Injectio

8 jab

9 Pure.DI

10 ServiceScan.SourceGenerator

See category

DependencyInjection