Clean Architecture Foundations & Project Setup
Part of Module 1: Enterprise Architecture & Solution Structuring
Learn how to structure an enterprise .NET 8 solution into Domain, Application, Infrastructure, and WebApi layers while enforcing the Inward Dependency Rule.
What You Will Master in This Lesson
- Separate business logic from SQL Server and HTTP frameworks cleanly
- Enforce compiler hygiene globally using Directory.Build.props
- Design encapsulated Domain entities using private setters and factory methods
1. Why Monolithic APIs Collapse at Scale
When an ASP.NET Core project starts small, putting EF Core DbContext calls directly inside API Controllers feels fast. Six months later, business rules are duplicated across five endpoints, changing a SQL column breaks HTTP responses, and unit testing requires spinning up a live database.
Clean Architecture solves this by organizing code around business policies rather than frameworks or databases. Databases and UI frameworks are treated as plugins to your core domain.
2. Enterprise Solution Folder Layout
An enterprise .NET 8 solution separates responsibilities into four distinct projects inside src/ plus automated tests inside tests/:
EnterpriseOrderPlatform/
├── Directory.Build.props (Centralized Nullable, ImplicitUsings & TreatWarningsAsErrors)
├── src/
│ ├── Core/
│ │ ├── Platform.Domain/ (Entities, Value Objects, Domain Events — ZERO NuGet deps)
│ │ └── Platform.Application/ (Interfaces, CQRS Commands/Queries, DTOs, Validators)
│ ├── Infrastructure/
│ │ └── Platform.Infrastructure/ (SQL Server, Stored Procs, Dapper, Redis, Email)
│ └── Presentation/
│ └── Platform.WebApi/ (Thin Controllers, Middleware, Program.cs)
└── tests/
├── Platform.Domain.UnitTests/
└── Platform.WebApi.IntegrationTests/
Platform.Domain references nothing. Platform.Application references only Domain. Platform.Infrastructure and Platform.WebApi implement the interfaces declared in Application.
3. Centralizing Compiler Hygiene with Directory.Build.props
Instead of configuring nullable reference types and strict warnings separately in every .csproj file, create a Directory.Build.props at the repository root so MSBuild applies them uniformly:
<Project>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
4. Rich Domain Entities vs Anemic Models
In amateur codebases, entities are just bags of public getters and setters. Any controller can overwrite customer.Email = "" or put an order into an illegal state. In Clean Architecture, setters are private, and mutations happen exclusively through validated factory or domain methods:
namespace Platform.Domain.Entities;
public sealed class Customer
{
public int Id { get; private set; }
public string FullName { get; private set; } = string.Empty;
public string Email { get; private set; } = string.Empty;
public bool IsActive { get; private set; }
public DateTime CreatedAtUtc { get; private set; }
// Parameterless private constructor reserved for ORM/Dapper materialization
private Customer() { }
public static Customer Create(string fullName, string email)
{
if (string.IsNullOrWhiteSpace(fullName))
throw new ArgumentException("Customer name cannot be empty.", nameof(fullName));
if (string.IsNullOrWhiteSpace(email) || !email.Contains('@'))
throw new ArgumentException("A valid email address is required.", nameof(email));
return new Customer
{
FullName = fullName.Trim(),
Email = email.Trim().ToLowerInvariant(),
IsActive = true,
CreatedAtUtc = DateTime.UtcNow
};
}
public void Deactivate()
{
if (!IsActive) return;
IsActive = false;
}
}
Common Pitfall
Installing Microsoft.Data.SqlClient or adding [Table("Customers")] attributes inside Platform.Domain. This leaks persistence concerns directly into core business models.
Enterprise Standard
Keep Platform.Domain 100% pure C#. Declare repository contracts like ICustomerRepository inside Platform.Application and implement SQL logic inside Platform.Infrastructure.
Hands-On Lab Exercise
Run the following .NET CLI commands in your terminal to scaffold the Clean Architecture skeleton:
mkdir EnterpriseOrderPlatform && cd EnterpriseOrderPlatform
dotnet new sln -n EnterpriseOrderPlatform
dotnet new classlib -n Platform.Domain -o src/Core/Platform.Domain
dotnet new classlib -n Platform.Application -o src/Core/Platform.Application
dotnet new classlib -n Platform.Infrastructure -o src/Infrastructure/Platform.Infrastructure
dotnet new webapi -n Platform.WebApi -o src/Presentation/Platform.WebApi
dotnet sln add src/Core/Platform.Domain src/Core/Platform.Application src/Infrastructure/Platform.Infrastructure src/Presentation/Platform.WebApi
dotnet add src/Core/Platform.Application reference src/Core/Platform.Domain
dotnet add src/Infrastructure/Platform.Infrastructure reference src/Core/Platform.Application
dotnet add src/Presentation/Platform.WebApi reference src/Core/Platform.Application src/Infrastructure/Platform.Infrastructure
Knowledge Check
Verify your understanding before advancing to the next lesson:
1. In Clean Architecture, which project is allowed to be referenced by the Domain layer?
2. Why should Domain entity properties use private setters with static Create(...) factory methods?