Introduction
When starting a professional software project, the first goal should not be limited to creating a few working endpoints. We must also consider how the application will change as it grows, how easily new developers can understand it, how business rules will be protected, and how strongly database or external service changes will affect the system.
Throughout this course, we will build a real-world application with ASP.NET Core and .NET 10. Our example project will be an e-commerce API where users can browse products, create orders, and manage the order process. In this first lesson, we will not create database tables or controllers yet. Instead, we will define the project boundaries, clarify layer responsibilities, and establish the correct direction of dependencies.
This approach requires more planning at the beginning, but it reduces maintenance, testing, and development costs as the application grows. The purpose of Clean Architecture is not to make you memorize a particular folder structure. Its purpose is to isolate business rules from technical details that are likely to change.
Real-world project scenario and requirements
Let us first define the core requirements of the system. A customer should be able to browse products, view product details, and create an order from selected products. Administrator users should be able to add products, update product information, and change order statuses.
Functional requirements
- List products and view product details.
- Create, update, and delete products.
- Register users and authenticate them.
- Create a cart or order.
- Calculate order lines and the total order amount.
- Allow administrators to update order status.
Non-functional requirements
- Business rules must not depend on a database technology.
- The application must be verifiable with unit tests.
- The API layer must not contain business logic directly.
- Errors must be handled centrally and consistently.
- Adding a new data access method must not break existing business rules.
- Dependencies between layers must be explicit and easy to inspect.
These requirements show that we need to design more than code that merely works. We need to create a system that can evolve safely.
What is Clean Architecture?
Clean Architecture is an architectural approach that separates an application according to responsibilities and makes business rules independent from external technical details. The database, web framework, user interface, file system, or third-party services are not placed at the center of the system. The business rules are placed at the center.
In this architecture, dependencies point from outer layers toward inner layers. An inner project does not directly know about an outer project. For example, the Domain layer should not reference Entity Framework Core, ASP.NET Core, or SQL Server. The Application layer should not need to know how HTTP requests are received or which database stores the data.
Core rule: Inner layers do not know the details of outer layers. Outer layers may implement contracts defined by inner layers.
Responsibilities of the layers
| Layer | Primary responsibility | Must not know about |
|---|---|---|
| Domain | Entities, value objects, business rules, and domain contracts | ASP.NET Core, EF Core, SQL Server, HTTP |
| Application | Use cases, DTOs, application services, validation, and abstractions | A specific database provider or controller details |
| Infrastructure | EF Core, repository implementations, external services, and technical integrations | Detailed API endpoint design |
| API/Presentation | HTTP requests, response models, authentication, and API concerns | Direct implementation of domain rules |
Domain layer
The Domain layer is the innermost part of the application. Fundamental entities such as Product, Order, and OrderItem are defined here. Business rules such as “an order total cannot be negative” or “an order cannot be created without items” should also be expressed in this layer or in domain-owned behavior.
The purpose of the Domain layer is not to copy database tables. Instead of creating classes that only contain properties, we should allow domain objects to protect their own valid state through meaningful behavior.
Application layer
The Application layer coordinates the use cases performed by the system. “List products,” “create an order,” and “update a product” can each be modeled as use cases. Request and response DTOs, application services, repository interfaces, and validation rules can be placed in this layer.
The Application layer does not connect directly to a database. Instead, it defines an abstraction such as IProductRepository. The actual implementation that uses EF Core belongs to the Infrastructure layer.
Infrastructure layer
Infrastructure is the outer layer where technical details live. DbContext, entity configuration classes, migrations, repository implementations, email services, and file storage components belong here. If SQL Server is replaced with another provider, most of the related changes should remain limited to this layer.
API layer
The API layer connects the HTTP world with the Application layer. A controller or endpoint receives a request, calls the appropriate application service, and creates an HTTP response. Instead of writing complex pricing, stock, or order rules inside controllers, those responsibilities should be delegated to inner layers.
SOLID and dependency direction
Clean Architecture naturally aligns with the SOLID principles. The Single Responsibility Principle states that a class should have one primary reason to change. If a controller converts HTTP models, connects to a database, and applies business rules, it has several responsibilities.
The Dependency Inversion Principle is especially important. High-level business logic should not directly depend on low-level technical details. Both should depend on abstractions. Therefore, we define an interface in the Application layer and implement it in Infrastructure. The connection is created at runtime by the ASP.NET Core Dependency Injection container.
public interface IProductRepository
{
Task<Product?> GetByIdAsync(int id, CancellationToken cancellationToken);
Task<IReadOnlyList<Product>> GetAllAsync(CancellationToken cancellationToken);
}Application code using this interface does not need to know whether data comes from EF Core, an in-memory collection, or another service. This also allows unit tests to use a fake or in-memory implementation instead of a real database.
Creating the .NET 10 solution structure
Now let us create the solution and its projects. You can use a terminal or Visual Studio. The following commands create one solution and four foundational projects:
mkdir ShopPlatform
cd ShopPlatform
dotnet new sln -n ShopPlatform
dotnet new classlib -n ShopPlatform.Domain
dotnet new classlib -n ShopPlatform.Application
dotnet new classlib -n ShopPlatform.Infrastructure
dotnet new webapi -n ShopPlatform.Api
dotnet sln add ShopPlatform.Domain/ShopPlatform.Domain.csproj
dotnet sln add ShopPlatform.Application/ShopPlatform.Application.csproj
dotnet sln add ShopPlatform.Infrastructure/ShopPlatform.Infrastructure.csproj
dotnet sln add ShopPlatform.Api/ShopPlatform.Api.csprojNext, add project references in the intended direction:
dotnet add ShopPlatform.Application reference ShopPlatform.Domain
dotnet add ShopPlatform.Infrastructure reference ShopPlatform.Application
dotnet add ShopPlatform.Infrastructure reference ShopPlatform.Domain
dotnet add ShopPlatform.Api reference ShopPlatform.Application
dotnet add ShopPlatform.Api reference ShopPlatform.InfrastructureIn this structure, Domain does not depend on another project. Application knows only Domain. Infrastructure knows Application and Domain. API knows Application and Infrastructure so that technical services can be registered at the application entry point.
The API reference to Infrastructure may initially appear to violate the inward dependency rule. However, this reference can be limited to the composition root, which is the startup location where dependency injection registrations are configured. Application itself must not know about Infrastructure.
Recommended folder structure
ShopPlatform/
├── ShopPlatform.sln
├── ShopPlatform.Domain/
│ ├── Entities/
│ ├── ValueObjects/
│ └── Common/
├── ShopPlatform.Application/
│ ├── Abstractions/
│ ├── DTOs/
│ ├── Features/
│ └── Behaviors/
├── ShopPlatform.Infrastructure/
│ ├── Persistence/
│ ├── Repositories/
│ └── DependencyInjection/
└── ShopPlatform.Api/
├── Controllers/
├── Middleware/
└── Extensions/Folder names are not mandatory. More important than naming is keeping responsibilities separate. In a small project, this many folders may initially feel excessive. However, each folder should represent a real responsibility and should not exist only to make the project look organized.
Common mistakes
- Adding EF Core to Domain: Do not make the Domain depend on ORM details merely to map entities to database tables.
- Writing business logic in the API: Model use cases in the Application layer instead of turning controllers into large service classes.
- Creating repositories for every operation: A repository should be used when it provides a meaningful abstraction, not simply because it is a familiar pattern.
- Seeing layers as folders only: Four folders do not create Clean Architecture. Dependency direction and responsibility boundaries are the real foundation.
- Making every class public: Keeping types internal when they do not need to be exposed can make boundaries clearer.
- Over-abstracting from day one: Generic repositories, many factories, and unnecessary base classes can make the design harder before a real need exists.
Short practice exercise
Complete the following exercise:
- Create the
ShopPlatformsolution and the four projects. - Verify that each project targets .NET 10.
- Add the references shown above.
- Add a
Productclass to the Domain project withId,Name,Description, andPriceproperties. - Add the
IProductRepositoryinterface to the Application project. - Build the solution without creating a real implementation in Infrastructure or an endpoint in API.
- Identify one reference that would point in the wrong direction and write down why it should not be added.
dotnet buildIf the build succeeds, the first architectural skeleton is ready. In later lessons, we will fill this skeleton with real domain behavior, use cases, and data access.
Conclusion
In this lesson, we separated the real project requirements, examined the purpose of Clean Architecture, and defined the responsibilities of the Domain, Application, Infrastructure, and API layers. The most important idea is that business rules should not depend on technical details such as a database or web framework that may change.
We also examined dependency inversion from SOLID, why repository interfaces are defined in inner layers, and how to create a .NET 10 solution structure. In the next lesson, we will design the Domain layer in detail and apply the differences between entities, value objects, and domain behavior.