Pattern Library
A catalogue of design patterns used in the Granit framework, organized by category.
Each pattern documents the general concept, how it is implemented in Granit, and references to the actual source files where the pattern is applied.
Architecture patterns
Section titled “Architecture patterns”| Pattern | Description |
|---|---|
| Module System | Topological loading with [DependsOn] |
| Hexagonal Architecture | Ports and Adapters for infrastructure decoupling |
| Layered Architecture | Domain / Application / Infrastructure separation |
| Middleware Pipeline | Dual ASP.NET Core + Wolverine pipeline |
| Event-Driven | IDomainEvent (local) + IIntegrationEvent (durable) |
| REPR | Minimal API Request-Endpoint-Response |
| CQRS | IReader / IWriter separation, ArchUnitNET enforcement |
| Anti-Corruption Layer | Isolation of Keycloak, S3, Brevo, FCM via internal DTOs |
Cloud and SaaS patterns
Section titled “Cloud and SaaS patterns”| Pattern | Description |
|---|---|
| Multi-Tenancy | 3 isolation strategies, soft dependency, async propagation |
| Feature Flags | Multi-level resolution Tenant to Plan to Default |
| Transactional Outbox | Atomic event publishing via Wolverine Outbox |
| Idempotency | Stripe-style HTTP idempotency with state machine |
| Pre-Signed URL | Direct-to-cloud S3 upload/download |
| Sidecar / Behavior | Context propagation via Wolverine Behaviors |
| Circuit Breaker and Retry | Standard resilience + Wolverine RetryWithCooldown |
| Cache-Aside | Double-check locking + HybridCache L1/L2 |
| Rate Limiting | Per-tenant rate limiting with dynamic quotas |
| Saga / Process Manager | GDPR export, import/export orchestrators |
| Fan-Out | Wolverine cascade for notifications and webhooks |
| Claim Check | Soft dependency IClaimCheckStore for large payloads |
| Bulkhead Isolation | Queue isolation, parallelism, tenant quotas |
GoF behavioral patterns
Section titled “GoF behavioral patterns”| Pattern | Description |
|---|---|
| Strategy | TenantIsolationStrategy, IBlobKeyStrategy, IStringEncryptionProvider |
| Chain of Responsibility | TenantResolverPipeline, blob validation |
| Command | SendWebhookCommand, RunMigrationBatchCommand |
| Template Method | GranitModule lifecycle, GranitValidator |
| State Machine | IdempotencyState, BlobStatus |
| Observer / Event | Wolverine implicit event subscription |
| Mediator | Wolverine message bus |
| Null Object | NullTenantContext, NullCacheValueEncryptor |
GoF creational patterns
Section titled “GoF creational patterns”| Pattern | Description |
|---|---|
| Factory Method | VaultClientFactory, DbContext tenant factories |
| Singleton | AsyncLocal singletons, NullTenantContext.Instance |
| Builder | Fluent AddGranit*() extensions |
GoF structural patterns
Section titled “GoF structural patterns”| Pattern | Description |
|---|---|
| Adapter | TypedKeyCacheServiceAdapter, S3BlobClient |
| Decorator | DistributedCacheService, CachedLocalizationOverrideStore |
| Proxy | FilterProxy for EF Core, Interceptors |
| Facade | DefaultBlobStorage, GranitExceptionHandler |
| Composite | Auditable entity hierarchy |
Data patterns
Section titled “Data patterns”| Pattern | Description |
|---|---|
| Repository | Store interfaces + EF Core / InMemory implementations |
| Soft Delete | ISoftDeletable + SoftDeleteInterceptor (GDPR) |
| Data Filtering | IDataFilter with ImmutableDictionary AsyncLocal |
| Unit of Work | Implicit DbContext + interceptor chain |
| Specification | QueryDefinition whitelist-first, expression trees |
Concurrency patterns
Section titled “Concurrency patterns”| Pattern | Description |
|---|---|
| Scope / Context Manager | using pattern for context restoration |
| Copy-on-Write | ImmutableDictionary for thread-safe state |
| Double-Check Locking | Anti-stampede on cache miss |
.NET idiom patterns
Section titled “.NET idiom patterns”| Pattern | Description |
|---|---|
| Expression Trees | Dynamic EF Core query filter construction |
| Marker Interface | ISoftDeletable, IMultiTenant, IDomainEvent |
| Options Pattern | 93 Options classes, ValidateOnStart |
Security patterns
Section titled “Security patterns”| Pattern | Description |
|---|---|
| Claims-Based Identity | JWT Keycloak + dynamic RBAC |
| Guard Clause | Systematic fail-fast, semantic exceptions |
Granit-specific variants
Section titled “Granit-specific variants”| Pattern | Description |
|---|---|
| Granit Variants | 10 hybrid patterns unique to Granit |