PatternKit Documentation
Fluent Design Patterns for Modern .NET
Elegant, declarative, allocation-light implementations of classic patterns—optimized for .NET 9.
🌟 Overview
PatternKit is a library that reimagines classic Gang of Four design patterns for the modern .NET ecosystem.
Instead of heavyweight base classes and verbose boilerplate, PatternKit favors:
- Fluent builders and chainable DSLs
- Source generators to remove reflection and runtime cost
- Zero-allocation handlers with
inparameters for hot paths - Strong typing and discoverable APIs
- Readable BDD-style tests using TinyBDD
🚀 Getting Started
Install the NuGet package:
dotnet add package PatternKit
Example: Strategy<TIn, TOut>
using PatternKit.Behavioral.Strategy;
var classify = Strategy<int, string>.Create()
.When(i => i > 0).Then(i => "positive")
.When(i => i < 0).Then(i => "negative")
.Default(_ => "zero")
.Build();
Console.WriteLine(classify.Execute(42)); // positive
Console.WriteLine(classify.Execute(-7)); // negative
Console.WriteLine(classify.Execute(0)); // zero
Example: TryStrategy<TIn, TOut>
var parser = TryStrategy<string, int>.Create()
.Always((in string s, out int r) => int.TryParse(s, out r))
.Finally((in string _, out int r) => { r = 0; return true; })
.Build();
if (parser.Execute("123", out var value))
Console.WriteLine(value); // 123
📚 Available Patterns
PatternKit will grow to cover Creational, Structural, and Behavioral patterns with fluent, discoverable APIs:
| Category | Patterns |
|---|---|
| Creational | Factory • Composer • ChainBuilder • BranchBuilder • MutableBuilder • Prototype • Singleton |
| Structural | Adapter • Bridge • Composite • Decorator • Facade • Flyweight • Proxy |
| Behavioral | Strategy • TryStrategy • ActionStrategy • ActionChain • ResultChain • Command • ReplayableSequence • WindowSequence • Mediator • Memento • Observer • AsyncObserver • Visitor • State • Template Method |
Each pattern will ship with:
- Fluent API: readable and composable (
.When(...),.Then(...),.Finally(...)) - Strongly-typed handlers using
inparameters - Performance focus — no hidden allocations or reflection
- XML docs and examples available in API reference
🧪 Testing Philosophy
All patterns are tested with TinyBDD, enabling Gherkin-like, human-readable scenarios:
[Feature("Strategy")]
public class StrategyTests : TinyBddXunitBase
{
[Scenario("Positive/negative classification")]
[Fact]
public async Task ClassificationWorks()
{
await Given("a strategy with three branches", BuildStrategy)
.When("executing with 5", s => s.Execute(5))
.Then("result should be 'positive'", r => r == "positive")
.AssertPassed();
}
}