Clean Architecture Solution Template with Domain-Driven Design (DDD)
This project provides a foundational template for building a robust and maintainable application using Clean Architecture principles. The architecture is specifically designed to align with Domain-Driven Design (DDD), where the application's core logic is centered around a rich domain model. This structure ensures that the business rules are isolated, testable, and independent of external frameworks or databases.
Domain-Centric Development: The Domains layer is the heart of the application. It contains the core business rules and logic, defined by entities and value objects that model the real-world domain.
Separation of Concerns: The project is strictly separated into layers, ensuring that each component has a single, well-defined responsibility. This prevents coupling and allows for independent development and testing of each layer.
Dependency Rule: Dependencies always point inward. The Infrastructure and Application layers depend on the Domain layer, but the Domain layer has no dependencies on any external components. This "inward-pointing" rule keeps the core business logic from being polluted by technology choices.
.
βββ clients/ # External client applications
β βββ External.Client.ApiConsumer/ # Console client consuming REST API
βββ samples/ # Sample projects and documentation
βββ src/ # Source code
β βββ Domains/
β β βββ CleanArchitecture.Domain/ # Domain entities, value objects, domain services
β βββ Applications/
β β βββ CleanArchitecture.Application/ # Use cases, interfaces, DTOs, business logic
β βββ Infrastructures/
β β βββ CleanArchitecture.Infrastructure/ # Core infrastructure implementations and shared adapters
β β βββ CleanArchitecture.Infrastructure.Azure/ # Azure-Cloud-specific infrastructure Adapter
β βββ Presentations/
β β βββ CleanArchitecture.Presentation.Api/ # RESTful API (ASP.NET Core)
β β βββ CleanArchitecture.Presentation.Console/ # Console presentation layer (template)
β β βββ CleanArchitecture.Presentation.Grpc/ # gRPC services
β β βββ CleanArchitecture.Presentation.Web/ # Web UI (Razor Pages)
β β βββ CleanArchitecture.Presentation.Jobs/ # Background jobs and workers (to be added)
βββ tests/
β βββ CleanArchitecture.Domain.UnitTests/ # Unit tests for Domain layer
βββ CleanArchitecture.sln # Solution file
βββ global.json # .NET SDK version configuration
βββ README.md
| Layer | Project | Description | Status | Dependencies |
|---|---|---|---|---|
| Domain | CleanArchitecture.Domain |
Enterprise business rules, entities, value objects, and domain services | β | None |
| Application | CleanArchitecture.Application |
Application business rules, use cases, and interfaces | β | Domain |
| Infrastructure | CleanArchitecture.Infrastructure |
Core infrastructure implementations (data access, repositories) | β | Application, Domain |
CleanArchitecture.Infrastructure.Azure |
Azure-specific implementations, Cloud SDK, connection secrets, cloud resource logic | Application, Domain | ||
| Presentation | CleanArchitecture.Presentation.Api |
RESTful API (ASP.NET Core Web API) | β | Application, Domain |
CleanArchitecture.Presentation.Console |
Console presentation layer template (optional) | Application, Domain | ||
CleanArchitecture.Presentation.Grpc |
gRPC services for high-performance communication | Application, Domain | ||
CleanArchitecture.Presentation.Web |
Web UI using Razor Pages | β | Application, Domain | |
CleanArchitecture.Presentation.Jobs |
Background jobs and workers for asynchronous processing | β | Application, Domain | |
| External Clients | External.Client.ApiConsumer |
External console client consuming REST API with dependency injection | β | None (API Consumer) |
| Symbol | Status | Description |
|---|---|---|
| β | Implemented | Ready to use, fully functional |
| Planned | Exists in codebase but may need completion | |
| β | Planned | Not yet implemented, future feature |
graph TD
A[Domain Layer]
B[Application Layer] --> A
C[Infrastructure Layer] --> B
C --> A
D[Presentation Layer] --> B
D --> A
Clean Architecture Principle: Dependencies always point inward toward the Domain layer, ensuring business logic remains independent of external concerns.
The separation of external clients from the core Clean Architecture provides several advantages:
- Independence: External clients are completely independent of the core business logic
- Flexibility: Clients can be developed in different technologies or frameworks
- Scalability: Multiple client types can consume the same API without affecting core architecture
- Maintainability: Client updates don't require changes to the core system
- Testing: Clients can be tested independently with mocked API responses
- Deployment: Clients can be deployed and versioned separately from the main application
The Domain layer includes:
- Value Objects: Rich domain objects with business logic (e.g., Color with RGB and opacity)
- Entities: Domain entities with business rules (e.g., Palette with color management)
- Common: Base classes like
ValueObjectfor implementing value object equality - Constants: Domain-specific constants
- Exceptions: Custom domain exceptions
The external console client demonstrates proper API consumption patterns:
- External Architecture: Separated from Clean Architecture layers as an independent client
- REST API Integration: Communicates with the Web API through HTTP client with proper error handling
- Dependency Injection: Uses Microsoft.Extensions.DependencyInjection for service registration
- Service Abstraction: Clean separation between UI, business logic, and API communication layers
- Structured Logging: Integrated Serilog for comprehensive logging and monitoring
- Configuration Management: Environment-specific settings with appsettings.json
- User Experience: Interactive menu-driven interface for palette management operations
Key Features:
- List palettes with pagination and search functionality
- Create, update, and delete palettes
- View detailed palette information with color visualization
- Add colors to palettes with RGB/Alpha value input
- Comprehensive error handling and user feedback
Usage:
# Start the API first
dotnet run --project src/Presentations/CleanArchitecture.Presentation.Api/
# Run the external client
dotnet run --project clients/External.Client.ApiConsumer/Comprehensive testing approach for each layer:
- Value Objects: Equality, immutability, business rules validation
- Entities: Business logic, invariants, domain events
- Domain Services: Complex business rules and domain logic
- Tools: xUnit, FluentAssertions, Shouldly
- Command Handlers: Business logic validation and side effects
- Query Handlers: Data retrieval and transformation logic
- Application Services: Workflow orchestration
- Tools: Moq for mocking, xUnit for test framework
- Repository Tests: Data access patterns with in-memory database
- Query Service Tests: Complex queries and data mapping
- Integration Tests: Database interactions and external services
- Tools: Entity Framework In-Memory provider, TestContainers
- Controller Tests: HTTP endpoints and request/response mapping
- Integration Tests: Full request pipeline testing
- API Contract Tests: OpenAPI specification validation
- Tools: ASP.NET Core TestServer, WebApplicationFactory
- API Client Tests: HTTP communication and error handling
- Service Integration Tests: Business logic and API interaction
- User Interface Tests: Console interaction and user experience
- End-to-End Tests: Full client-to-API workflow validation
- Tools: HttpClient mocking, xUnit, FluentAssertions
- β Clean Architecture structure with proper layer separation
- β .NET 8.0 support
- β Multiple presentation options (Web API, gRPC, Razor Pages)
- β External client applications (Console API Consumer)
- β C# 12.0 features with global using statements
- β Domain-Driven Design (DDD) implementation
- β CQRS pattern with Command/Query handlers
- β Value Objects with business logic (e.g., Color with RGB and opacity)
- β Unit testing setup with xUnit, FluentAssertions, and Shouldly
- β External console client with proper API consumption patterns
- β Azure infrastructure support
- β Dependency Injection configuration per layer
- β Structured logging with Serilog
- β Multiple environment configurations
- β Comprehensive TDD testing strategies for all layers
- .NET 8.0 SDK
- Your favorite IDE (Visual Studio, VS Code, JetBrains Rider)
dotnet builddotnet run --project src/Presentations/CleanArchitecture.Presentation.Api/The API will be available at https://localhost:7001 and http://localhost:5001
# In a new terminal window
dotnet run --project clients/External.Client.ApiConsumer/Web UI (Razor Pages):
dotnet run --project src/Presentations/CleanArchitecture.Presentation.Web/gRPC Service:
dotnet run --project src/Presentations/CleanArchitecture.Presentation.Grpc/If using Entity Framework Core with SQL Server:
# Add migration
dotnet ef migrations add InitialCreate \
-p src/Infrastructures/CleanArchitecture.Infrastructure/ \
-s src/Presentations/CleanArchitecture.Presentation.Api/ \
-o DataAccess/Migrations/
# Update database
dotnet ef database update \
-p src/Infrastructures/CleanArchitecture.Infrastructure/ \
-s src/Presentations/CleanArchitecture.Presentation.Api/# Run all tests
dotnet test
# Run specific test project
dotnet test tests/CleanArchitecture.Domain.UnitTests/
# Run tests with coverage
dotnet test --collect:"XPlat Code Coverage"This project is licensed with the MIT license.