Clean Architecture & DDD Mastery

Specification Pattern: Encapsulating business rules

1 Views Updated 5/4/2026

Reusable Logic

The Specification Pattern allows you to encapsulate a piece of business logic (usually a filter or validation rule) into a reusable class.

1. Solving the "Leaky Query"

Instead of writing .Where(o => o.IsPaid && o.Status == 2) in 5 different places, you create a PendingOrdersSpecification. You can then pass this specification to your repository. This ensures that the 'Definition' of a pending order is centralized in the Domain layer.

2. Logic Composition

Specifications can be combined using And(), Or(), and Not(). This allows you to build complex business rules out of simple, testable blocks. var spec = new OverdueSpec().And(new HighValueSpec());. It translates perfectly to EF Core Expression trees for high-performance database queries.

3. Architect Insight

Q: "When should I use Specifications?"

Architect Answer: "Use them when a boolean business rule is reused in multiple places (filtering a list, validating a single entity, or even determining if a discount applies). It prevents the 'Copy-Paste' of business logic and ensures your domain invariants are enforced consistently across the whole system."

Clean Architecture & DDD Mastery
1. Architectural Patterns
The Evolution of Architecture: Monolith to Clean Onion Architecture: Dependency Inversion at the core Clean Architecture: The 'Screaming' architecture Hexagonal Architecture (Ports and Adapters)
2. Domain-Driven Design (DDD) Foundations
Ubiquitous Language: Aligning code with business Entities vs Value Objects: Managing identity and state Aggregates & Aggregate Roots: Defining consistency boundaries Bounded Contexts: Handling complexity in large domains
3. Advanced DDD Patterns
Domain Services: When logic doesn't fit in an entity Domain Events: Decoupling side effects via events Repositories: Mediating between domain and data Unit of Work: Ensuring atomic transactions
4. Implementing the Clean Layers
The Domain Layer: Zero dependencies, pure C# The Application Layer: Orchestrating use cases The Infrastructure Layer: Bridging to the outside world The Presentation Layer: Decoupling the UI from logic
5. Patterns for Data & Logic
CQRS (Command Query Responsibility Segregation) MediatR: Implementing the Mediator pattern in .NET Specification Pattern: Encapsulating business rules Policy Pattern: Handling complex authorization rules
6. Enterprise Domain Challenges
Handling Persistence Ignorance with EF Core Mapping Layers: AutoMapper vs Manual Mapping Validation Strategies: FluentValidation in the App Layer Error Handling: Result patterns vs Exceptions
7. Testing Clean Architecture
Unit Testing the Domain: Fast and pure Testing Use Cases with Mocks Integration Testing the Infrastructure ArchUnit .NET: Enforcing architectural rules via tests
8. Real-World Case Study
Refactoring a 'Spaghetti' Monolith to Clean Architecture DDD in Action: Modeling a complex Logistics system