Factory Method Generator
Convert a static partial class into a keyed factory using [FactoryMethod], [FactoryCase], and [FactoryDefault]. The generator produces Create, TryCreate, and async siblings when needed.
Quick start
using PatternKit.Generators.Factories;
namespace Demo;
[FactoryMethod(typeof(string), CreateMethodName = "Make")]
public static partial class MimeFactory
{
[FactoryCase("json")]
public static string Json() => "application/json";
[FactoryDefault]
public static string Default() => "application/octet-stream";
}
Generated API (sync):
public static string Make(string key);
public static bool TryCreate(string key, out string value);
Async is generated when any case/default returns ValueTask<T> or Task<T>:
public static ValueTask<string> MakeAsync(string key);
public static ValueTask<(bool Success, string Result)> TryCreateAsync(string key);
Behavior
- Key matching
KeyType == stringandCaseInsensitiveStrings == true(default) →String.EqualswithOrdinalIgnoreCase.- Otherwise exact match (
Ordinalfor strings).
- Default path
- If
[FactoryDefault]exists, it is used when no case matches. - Otherwise
CreatethrowsArgumentOutOfRangeException;TryCreatereturnsfalseanddefault.
- If
- Parameters
- All
[FactoryCase]/[FactoryDefault]methods must bestaticwith identical signatures (return + parameters). Parameters are forwarded into generated methods.
- All
- Async rules
- If any mapping is async, generated async APIs use
ValueTask<T>. Task<T>cases are wrapped inValueTask<T>.- Synchronous cases are wrapped with
ValueTask.FromResult.
- If any mapping is async, generated async APIs use
Diagnostics
| Id | Message | Fix |
|---|---|---|
| PKKF001 | [FactoryMethod] type must be static partial |
Mark the class static partial. |
| PKKF002 | Factory methods must share the same signature | Align return type and parameters across all cases/default. |
| PKKF003 | Duplicate factory key | Remove or rename the duplicate key. |
| PKKF004 | Multiple default factory methods | Keep only one [FactoryDefault]. |
| PKKF005 | Invalid factory key value | Ensure the key literal converts implicitly to KeyType. |
| PKKF006 | Factory methods must be static | Mark all [FactoryCase]/[FactoryDefault] methods static. |
Tips
- Prefer small, pure mapping methods; let the generated factory handle validation and branching.
- For string keys that must be case-sensitive, set
CaseInsensitiveStrings = falseand test bothCreateandTryCreate. - Use
CreateMethodNameto match existing APIs (Make,FromKey, etc.).