FactoryGenie 1.0.0
dotnet add package FactoryGenie --version 1.0.0
NuGet\Install-Package FactoryGenie -Version 1.0.0
<PackageReference Include="FactoryGenie" Version="1.0.0" />
<PackageVersion Include="FactoryGenie" Version="1.0.0" />
<PackageReference Include="FactoryGenie" />
paket add FactoryGenie --version 1.0.0
#r "nuget: FactoryGenie, 1.0.0"
#:package FactoryGenie@1.0.0
#addin nuget:?package=FactoryGenie&version=1.0.0
#tool nuget:?package=FactoryGenie&version=1.0.0
FactoryGenie
Compile-time factory generation for your C# projects.
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:
- If you rename, add, or remove parameters, it breaks. And even worse, the error will occur at runtime rather than compile time.
- 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.
- 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.
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.