FactoryGenie 1.0.0

dotnet add package FactoryGenie --version 1.0.0
                    
NuGet\Install-Package FactoryGenie -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FactoryGenie" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FactoryGenie" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="FactoryGenie" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FactoryGenie --version 1.0.0
                    
#r "nuget: FactoryGenie, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FactoryGenie@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FactoryGenie&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=FactoryGenie&version=1.0.0
                    
Install as a Cake Tool

FactoryGenie

Compile-time factory generation for your C# projects.

alternate text is missing from this package README image

By marking up your classes with attributes, a lightweight and performant factory class will be generated for you. You get the runtime performance of compiled C# code, without the maintenance work associated with creating and updating the factory class yourself.

You write:

[GenerateFactory]
public class MyClass
{
    public MyClass(string stringParam, [FactoryParam] bool boolParam, double doubleParam)
    {

    }

    public MyClass(int intParam, string stringParam, [FactoryParam] float floatParam)
    {

    }
}

FactoryGenie generates:

public class MyClassFactory
{
    private readonly string stringParam;
    private readonly double doubleParam;
    private readonly int intParam;

    public MyClassFactory(string stringParam, double doubleParam, int intParam)
    {
        this.stringParam = stringParam;
        this.doubleParam = doubleParam;
        this.intParam = intParam;
    }

    public MyClass Create(bool boolParam)
    {
        return new MyClass(stringParam, boolParam, doubleParam);
    }

    public MyClass Create(float floatParam)
    {
        return new MyClass(intParam, stringParam, floatParam);
    }
}

As you can see:

  • You mark a class you want to generate a factory for with the [GenerateFactory] attribute.
  • It creates a class which has the name of your class suffixed with "Factory". It has a method called "Create" which you can call to generate an instance of your class.
  • Parameters marked with [FactoryParam] attribute do not get put in the constructor, but instead will be passed via the create method.
  • You can have as many constructors as you want, it will merge identical parameters into the factory constructor.
  • Since the generated factory is a simple class, you can use it with literally any dependency injection container.

Dependency Injection

When creating a transient object via dependency injection, you need some way to pass your parameters to the instance being constructed, but you also want it to get the services it needs from the container. Historically, the most common solution for this has been "Parameter overrides", which looked something like this:

delegate IService ServiceFactory(string name);

container.Register<ServiceFactory>(
    name => container.Resolve<IService>(new ParameterOverride("name", name))
    );

This is good because unlike directly calling the constructor, if services get added or removed, you don't have to worry about updating anywhere, it's one of the most underrated benefits of dependency injection.

The problem with this however is threefold:

  1. If you rename, add, or remove parameters, it breaks. And even worse, the error will occur at runtime rather than compile time.
  2. If you remember that it's gonna break, you still have to go and fix it, adding the new parameter in both the factory delegate definition, and the Resolve call.
  3. Resolving at runtime either requires some initial cost to do runtime compilation, or does the significantly slower reflection-based construction.

FactoryGenie solves all of these issues. It generates the factory for you, updating automatically whenever something changes, and this is done at compile time so there's no runtime cost.

Using the factory it creates is as simple as:

container.RegisterSingleton<ServiceFactory>();
var factory = container.Resolve<ServiceFactory>();
var instance = factory.Create("name");

Responding to the 3-Wish Allegations

This is an excerpt from an interview with the Factory Genie:

There's been a terrible myth circulating that I can only generate 3 factories for you then I stop working. This is completely false, I can generate as many factories as you need, your harddrive will be filled to the brim with factories. This falsehood has been perpetuated by my brother. That lazy piece of shit cant be bothered so made up an arbitrary limitation to his powers, now he's off relaxing on a beach in Marbella. Me, I work hard every day generating factories, and I love my job, I love the feeling of fulfillment from helping people. So no, there is no 3-wish limit, unlike my lazy brother I actually give a fac.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

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.0 33 4/4/2026

Initial public release.