Introduction
SAPFIAI is built using Clean Architecture principles, creating a maintainable, testable, and scalable API solution. The architecture separates concerns into distinct layers, with dependencies flowing inward toward the domain core.Architecture Layers
The solution is organized into four main layers:Domain Layer
Core business logic and entities. No external dependencies.
Application Layer
Use cases, CQRS commands/queries, and business orchestration.
Infrastructure Layer
Data persistence, external services, and third-party integrations.
Web Layer
API endpoints, middleware, and HTTP concerns.
Dependency Flow
The architecture enforces a strict dependency rule:Dependencies point inward. The Domain layer has zero external dependencies, while outer layers depend on inner layers through abstractions (interfaces).
Visual Representation
Layer Interactions
Request Flow Example
Let’s follow a typical API request through the layers:Key Benefits
Testability
Testability
Each layer can be tested independently:
- Domain: Pure unit tests with no mocking
- Application: Test use cases with mocked dependencies
- Infrastructure: Integration tests with real databases
- Web: Functional tests for end-to-end scenarios
Maintainability
Maintainability
- Clear separation of concerns
- Each layer has a single responsibility
- Easy to locate and modify code
- Changes in one layer rarely affect others
Flexibility
Flexibility
- Swap implementations without changing business logic
- Easy to switch databases (SQL Server → PostgreSQL)
- Change UI frameworks without touching the core
- Add new features without modifying existing code
Scalability
Scalability
- Independent deployment of layers
- Microservices-ready architecture
- Horizontal scaling of specific components
- Performance optimization per layer
Technology Stack
The architecture leverages modern .NET technologies:| Layer | Technologies |
|---|---|
| Domain | .NET 8, C# Records |
| Application | MediatR, FluentValidation, AutoMapper |
| Infrastructure | Entity Framework Core 8, SQL Server, ASP.NET Identity |
| Web | ASP.NET Core 8, Minimal APIs, Swagger/OpenAPI |
Cross-Cutting Concerns
Several patterns span multiple layers:MediatR Pipeline Behaviors
The application uses pipeline behaviors for cross-cutting concerns:- Exception handling: Catches and logs unhandled exceptions
- Authorization: Validates user permissions before execution
- Validation: Runs FluentValidation rules automatically
- Performance: Logs slow requests for monitoring
Next Steps
Clean Architecture
Deep dive into Clean Architecture principles
Project Structure
Detailed breakdown of folders and files
CQRS Pattern
Learn about Commands and Queries
Getting Started
Start building with SAPFIAI