Design Patterns for .NET
Apply essential design patterns in .NET Core — dependency injection patterns, behavioral patterns, and enterprise integration patterns.
55 min•By Priygop Team•Last updated: Feb 2026
Essential .NET Patterns
- Repository Pattern: Abstract data access behind interfaces — IOrderRepository.GetByIdAsync(). Enables testing with in-memory implementations, testability
- Unit of Work: Coordinate multiple repository operations in one transaction — IUnitOfWork.SaveChangesAsync() commits all changes atomically
- Strategy Pattern: Define a family of algorithms, encapsulate each, make interchangeable — IDiscountStrategy with PercentageDiscount, FlatDiscount, BulkDiscount
- Decorator Pattern: Add behavior to objects dynamically — CachingOrderRepository wraps SqlOrderRepository, adding cache layer without modifying original
- Factory Pattern: Encapsulate object creation logic — PaymentProcessorFactory.Create(paymentType) returns the correct implementation based on input
- Specification Pattern: Encapsulate query criteria — ActiveOrdersSpec, HighValueCustomersSpec. Composable, reusable, testable query logic in domain layer
- Result Pattern: Replace exceptions for expected failures — Result<Order, Error> makes failure handling explicit. No try-catch for business validation
Enterprise Integration Patterns
- API Versioning: URL-based (/api/v1/orders), header-based (api-version: 2.0), or query string (?api-version=1.0). Support old versions for 6-12 months minimum
- Idempotency: Ensure operations can be safely retried — use idempotency keys for POST requests. If Idempotency-Key header matches, return cached response
- Correlation ID: Pass X-Correlation-Id through all service calls — links logs, traces, and events for a single user request across multiple services
- Circuit Breaker (Polly): Prevent cascade failures — after N consecutive failures, stop calling the failing service for a timeout period. Fail fast, recover gracefully
- Retry with Backoff: Exponential backoff with jitter — 1s, 2s, 4s + random. Prevents thundering herd when a service recovers
- Bulkhead Isolation: Limit concurrent calls to each dependency — if PaymentService hangs, it shouldn't consume all your threads and block InventoryService calls