Table of Contents

Examples & Demos

Welcome! This section collects small, focused demos that show how to compose behaviors with PatternKit—without sprawling frameworks, if/else ladders, or tangled control flow. Each demo is production-shaped, tiny, and easy to lift into your own code.

What you'll see

  • First-match-wins strategies for branching without if chains.
  • Branchless action chains for rule packs (logging, pre-auth, discounts, tax).
  • Fluent decorators for layering functionality (tax, discounts, rounding, logging) without inheritance.
  • Pipelines built declaratively and tested end-to-end.
  • Config-driven composition (DI + IOptions) so ops can re-order rules without redeploys.
  • Enterprise feature slices wired through the standard .NET DI container with PatternKit artifacts registered as singletons.
  • Strategy-based coercion for turning "whatever came in" into the types you actually want.
  • Ultra-minimal HTTP routing to illustrate middleware vs. routes vs. negotiation.
  • Flyweight identity sharing to eliminate duplicate immutable objects (glyphs, styles, tokens).
  • Snapshot history & undo/redo (Memento) for document/editor style workflows.
  • Source-generated application wiring for builders, factories, facades, proxies, observers, visitors, state machines, strategies, mementos, template methods, and messaging factories.
  • Enterprise messaging workflows for envelopes, routers, recipient lists, splitters, aggregators, routing slips, sagas, mailboxes, idempotent receivers, inboxes, and outboxes.
  • Messaging backplane facade for host-style setup, typed request/reply, and publish/subscribe over an application-owned transport boundary.
  • Production-readiness catalog for DI, generic host, and ASP.NET Core diagnostics that maps every documented example to its source, TinyBDD tests, docs page, integration surfaces, and production checks.
  • Workflow orchestration for explicit ordered fulfillment steps with retries, conditional gates, compensation, and execution history.
  • Snapshot / checkpoint management for resumable event stream replay and projection rebuilds.
  • Eventual consistency monitoring for projection and integration lag visibility.

Demos in this section

  • Composed, Preference-Aware Notification Strategy (Email/SMS/Push/IM) Shows how to layer a user's channel preferences, failover, and throttling into a composable Strategy without switches. Good template for "try X, else Y" flows (alerts, KYC, etc.).

  • Auth & Logging Chain A tiny ActionChain<HttpRequest> showing request ID logging, an auth short-circuit for /admin/*, and the subtleties of .ThenContinue vs .ThenStop vs Finally (strict-stop semantics by default).

  • Strategy-Based Data Coercion Coercer<T> compiles a tiny set of TryStrategy handlers once per target type to coerce JSON/primitives/strings at runtime—first-match-wins, culture-safe, allocation-light.

  • Mediated Transaction Pipeline An in-code pipeline (no config) that demonstrates ActionChain, Strategy, and TryStrategy together: pre-auth checks, discounting, tax, rounding, tender handling, and finalization. Emphasizes clear logs and testable rules.

  • Configuration-Driven Transaction Pipeline Same business shape as above, but wired via DI + IOptions<PipelineOptions>. Discounts/rounding/tenders are discovered and ordered from config, making the pipeline operationally tunable.

  • Enterprise Feature Slices with .NET DI A checkout feature slice registered with Microsoft.Extensions.DependencyInjection, exposing a typed IEnterpriseCheckout facade while the container owns Flyweight, Factory, Prototype, Chain, Strategy, Decorator, Abstract Factory, Proxy, TypeDispatcher, State Machine, Memento, Observer, and Flow artifacts. See Enterprise Feature Slices with .NET DI.

  • Fulfillment Workflow Orchestration A Generic Host importable fulfillment workflow with fluent and source-generated routes for inventory reservation, fraud review, payment capture, retries, warehouse release, and compensation. See Fulfillment Workflow Orchestration.

  • Order Replay Snapshot Checkpoint Management A Generic Host importable replay service with fluent and source-generated checkpoint manager routes for event-sourced order rebuilds. See Order Replay Snapshot Checkpoint Management.

  • Order Projection Eventual Consistency Monitor A Generic Host importable monitor with fluent and source-generated routes for source/target watermark convergence. See Order Projection Eventual Consistency Monitor.

  • Customer Notification Null Object A Generic Host importable notification fallback with fluent and source-generated routes, IServiceCollection registration, and deterministic no-op behavior for optional collaborators. See Customer Notification Null Object.

  • Checkout Backpressure A Generic Host importable checkout admission gate with fluent and source-generated routes, IServiceCollection registration, and explicit saturation behavior. See Checkout Backpressure.

  • Customer Profile Lazy Load A Generic Host importable deferred profile lookup with fluent and source-generated routes, IServiceCollection registration, TTL caching, and invalidation. See Customer Profile Lazy Load.

  • Order Entry Ports and Adapters A Generic Host importable hexagonal order-entry flow with fluent and source-generated adapter routes. See Order Entry Ports and Adapters.

  • Product Catalog Change Data Capture A Generic Host importable mutation capture workflow with fluent and source-generated routes, ordered pending records, and post-commit publication. See Product Catalog Change Data Capture.

  • Minimal Web Request Router A tiny "API gateway" that separates first-match middleware (side effects/logging/auth) from first-match routes and content negotiation. A crisp example of Strategy patterns in an HTTP-ish setting.

  • Payment Processor — Fluent Decorator Pattern for Point of Sale Demonstrates the Decorator pattern for building flexible payment processors. Shows how to layer tax calculation, promotional discounts, loyalty programs, employee benefits, and rounding strategies on a base processor—no inheritance hierarchies. Includes five real-world processors (simple, retail, e-commerce, cash register, birthday special) with full test coverage. Perfect for understanding decorator execution order and composition patterns.

  • Flyweight Glyph Cache & Style Sharing Shows a high-volume text/glyph layout where each distinct glyph (and style) is allocated once and reused. Demonstrates intrinsic vs extrinsic state separation, preload of hot keys (spaces), custom key comparers (case-insensitive styles), and thread-safe lazy creation—mirroring classic Flyweight scenarios (rendering, AST token metadata, icon caches).

  • Document Editing History (Memento) A simple document buffer with tagged snapshots, undo/redo, jump-to-version, and duplicate suppression illustrating the Memento pattern's practical shape in a UI/editor workflow.

  • State Machine — Order Lifecycle A fluent state machine driving an order lifecycle with entry/exit hooks, transition effects, and default per‑state behavior. Shows determinism (first‑match wins), internal (Stay) vs cross‑state transitions, and log/audit side‑effects.

  • Template Method Data Processor Shows how to use the Template Method pattern to define a reusable, extensible workflow for data processing. Demonstrates hooks for pre/post processing, thread-safety, and easy extensibility. See Template Method Demo.

  • Template Method Async Pipeline End-to-end async pipeline (fetch → transform → store) with cancellation, optional synchronization, and error observation. Shows both subclassing (AsyncTemplateMethod) and fluent (AsyncTemplate) approaches. See Template Method Async Demo.

  • Source Generator Application Suite A production-shaped generator suite covering DI module wiring, corporate host composition, generated facades, generated proxies, generated observers, generated mementos, generated state machines, generated strategies, generated visitors, generated template methods, and messaging generators. See Source Generator Application Suite.

  • Enterprise Messaging Workflow Suite End-to-end messaging examples for envelopes, content routing, recipient lists, splitters, aggregators, routing slips, sagas, mailboxes, idempotent receivers, inboxes, outboxes, and generated messaging factories. See Enterprise Messaging Workflow Suite.

  • Order Event-Driven Consumer Shows fluent and source-generated push consumers side by side, with an importable IServiceCollection extension. See Order Event-Driven Consumer.

  • ERP Channel Adapter Shows fluent and source-generated external DTO adapters over PatternKit message channels, with an importable IServiceCollection extension. See ERP Channel Adapter.

  • Payment Messaging Gateway Shows fluent and source-generated request/response gateways over PatternKit message channels, with an importable IServiceCollection extension. See Payment Messaging Gateway.

  • Inventory Service Activator Shows fluent and source-generated message-to-service activation with an importable IServiceCollection extension. See Inventory Service Activator.

  • Fulfillment Priority Queue Shows fluent and source-generated business-priority queues with an importable IServiceCollection extension. See Fulfillment Priority Queue.

  • Fulfillment Health Endpoint Monitoring Shows fluent and source-generated health checks with IServiceCollection, Generic Host-friendly services, and ASP.NET Core route mapping. See Fulfillment Health Endpoint Monitoring.

  • Customer Dashboard Gateway Aggregation Shows fluent and source-generated API gateway aggregation with IServiceCollection and ASP.NET Core endpoint mapping. See Customer Dashboard Gateway Aggregation.

  • Generated Message Envelope Shows fluent and source-generated message envelope contracts side by side, with an importable IServiceCollection extension. See Generated Message Envelope.

  • Order Canonical Data Model Shows fluent and source-generated order normalization into an application-owned canonical contract, with an importable IServiceCollection extension. See Order Canonical Data Model.

  • Inventory Event-Carried State Transfer Shows fluent and source-generated inventory projection events that carry enough state to update a local read model, with an importable IServiceCollection extension. See Inventory Event-Carried State Transfer.

  • Order Event Notification Shows fluent and source-generated compact order notifications with correlation metadata, dispatch rules, and an importable IServiceCollection extension. See Order Event Notification.

  • Generated Recipient List Shows fluent and source-generated recipient-list fan-out side by side, with an importable IServiceCollection extension. See Generated Recipient List.

  • Generated Splitter and Aggregator Shows fluent and source-generated split/rejoin message routing side by side, with an importable IServiceCollection extension. See Generated Splitter And Aggregator.

  • Generated Mailbox Shows fluent and source-generated serialized inboxes side by side, with an importable IServiceCollection extension. See Generated Mailbox.

  • Generated Reliability Pipeline Shows fluent and source-generated idempotent receiver, inbox, and outbox composition side by side, with an importable IServiceCollection extension. See Generated Reliability Pipeline.

  • Resilient Checkout and Collaborating Mailboxes Application-shaped messaging demos: checkout route selection, routing-slip execution, command compensation, fallback routes, and service mailboxes collaborating over correlated messages. See Resilient Checkout and Collaborating Mailboxes.

  • Messaging Backplane Facade Demonstrates how PatternKit can sit behind a MassTransit- or MediatR-style host builder and typed client while RabbitMQ, Azure Service Bus, Postgres, MQTT, or another adapter remains infrastructure-owned. See Messaging Backplane Facade.

  • Production-Ready Example Integrations Importable catalog and validation tooling for every documented example. Register it through IServiceCollection, validate it during generic host startup, or expose it through ASP.NET Core minimal API endpoints. See Production-Ready Example Integrations.

How to run

From the repo root:

# Build everything
dotnet build PatternKit.slnx -c Release

# Run all tests (quick, cross-targeted)
dotnet test PatternKit.slnx -c Release

Tip (Linux/macOS): our tests force en-US culture to make currency/text output stable across platforms. If you run outside the test host, ensure invariant globalization isn’t enabled:

  • DOTNET_SYSTEM_GLOBALIZATION_INVARIANT should be unset or 0.
  • To see console output, execute demo entrypoints (e.g., Demo.Run) from your IDE or a small console host.

Design highlights

  • First-match wins everywhere (middleware, routes, rounding rules, tender routers).
  • Zero-if routing via BranchBuilder and delegates (PredicateHandler).
  • Branchless rule packs via ActionChain<T> with When/ThenContinue/ThenStop/Finally.
  • Immutable, thread-safe artifacts after .Build(); builders remain mutable.
  • Tiny domain types you can replace or extend (requests, responses, tenders, items, rules).
  • Identity sharing via Flyweight to lower allocation pressure in repetition-heavy domains.

Where to look (quick map)

  • Auth & Logging Chain: AuthLoggingDemo (+ AuthLoggingDemoTests) — strict-stop auth with logging.
  • Coercer: Coercer<T> (+ CoercerTests) — strategy-based, culture-safe coercion.
  • Mini Router: MiniRouter + Demo.Run — middleware/auth/negotiation in console output.
  • Mediated Pipeline: TransactionPipelineBuilder + MediatedTransactionPipelineDemo.Run.
  • Config-Driven Pipeline: ConfigDrivenPipelineDemo.AddPaymentPipeline + PipelineOptions.
  • Enterprise Feature Slices: EnterpriseFeatureSlicesDemo.AddEnterpriseFeatureSlices (+ EnterpriseFeatureSlicesDemoTests) — standard .NET DI owns PatternKit artifacts behind a typed checkout facade.
  • Payment Processor Decorators: PaymentProcessor* + related tests.
  • Flyweight Glyph Cache: FlyweightDemo (+ FlyweightDemoTests) — glyph width layout & style sharing.
  • Flyweight Structural Tests: Structural/Flyweight/FlyweightTests.cs — preload, concurrency, comparer, guards.
  • Memento Document Demo: MementoDemo — buffer edits with undo/redo and tags.
  • State Machine Demo: OrderStateDemo (+ StateDemoTests) — order lifecycle with entry/exit hooks and default behaviors.
  • Source Generator Suite: src/PatternKit.Examples/Generators (+ PatternKit.Examples.Tests/Generators) — generated builders, factories, facades, mementos, state machines, strategies, visitors, and host-style composition.
  • Generator Boundary Demos: AdapterGeneratorDemo, ObserverGeneratorDemo, ProxyGeneratorDemo, SingletonGeneratorDemo, TemplateMethodGeneratorDemo (+ matching tests) — generated adapters, observers, proxies, singletons, and template workflows.
  • Enterprise Messaging: src/PatternKit.Examples/Messaging (+ PatternKit.Examples.Tests/Messaging) — runtime and generated messaging workflows.
  • Resilient Checkout: ResilientCheckoutDemo (+ ResilientCheckoutDemoTests) — fallback checkout routes with compensation.
  • Collaborating Mailboxes: ServiceCollaborationMailboxDemo (+ ServiceCollaborationMailboxDemoTests) — inventory, payment, shipping, and notification services collaborating through serialized mailboxes.
  • Messaging Backplane Facade: BackplaneFacadeDemo (+ BackplaneFacadeDemoTests) — host setup, request/reply, pub/sub, outbox, idempotency, and mailbox-backed transport subscribers.
  • Product Gateway Routing: ProductGatewayRoutingDemo (+ ProductGatewayRoutingDemoTests) — fluent and generated downstream route dispatch with DI and ASP.NET Core mapping.
  • Checkout Strangler Fig Migration: CheckoutStranglerFigDemo (+ CheckoutStranglerFigDemoTests) — fluent and generated legacy-to-modern checkout routing with DI and ASP.NET Core mapping.
  • Order Telemetry Sidecar: OrderTelemetrySidecarDemo (+ OrderTelemetrySidecarDemoTests) — fluent and generated companion telemetry behavior with DI and ASP.NET Core mapping.
  • Commerce Backends for Frontends: CommerceBackendsForFrontendsDemo (+ CommerceBackendsForFrontendsDemoTests) — fluent and generated client-specific facade shaping with DI and ASP.NET Core mapping.
  • Inventory Ambassador: InventoryAmbassadorDemo (+ InventoryAmbassadorDemoTests) — fluent and generated outbound connectivity wrapper with DI and ASP.NET Core mapping.
  • Warehouse Leader Election: WarehouseLeaderElectionDemo (+ WarehouseLeaderElectionDemoTests) — fluent and generated active worker lease coordination with DI and Generic Host mapping.
  • Order Allocation Distributed Lock: OrderAllocationDistributedLockDemo (+ OrderAllocationDistributedLockDemoTests) — fluent and generated resource lease coordination with DI and Generic Host mapping.
  • Checkout Compensating Transaction: CheckoutCompensatingTransactionDemo (+ CheckoutCompensatingTransactionDemoTests) — fluent and generated reversible checkout steps with DI and Generic Host mapping.
  • Warehouse Scheduler Agent Supervisor: WarehouseSchedulerAgentSupervisorDemo (+ WarehouseSchedulerAgentSupervisorDemoTests) — fluent and generated scheduled worker supervision with DI and Generic Host mapping.
  • Production-Ready Example Catalog: PatternKitExampleCatalog (+ PatternKitExampleCatalogTests) — DI registration, generic host validation, ASP.NET Core endpoint mapping, and source/test/docs manifest checks.
  • Tests: PatternKit.Examples.Tests/* use TinyBDD scenarios that read like specs.

Why these demos exist

They’re meant to be copy-pasteable patterns:

  • Replace cascade if/else with composed strategies.
  • Turn scattered rules into a linear, testable chain.
  • Move “what runs & in what order” to configuration, when appropriate.
  • Keep the primitives small so the system stays legible under change.
  • Eliminate duplicate immutable instances (Flyweight) where repetition is high.

Jump in via the pages in the left-hand ToC and open the corresponding test files—the assertions double as executable documentation.