What Is Clean Architecture?

Learn What Is Clean Architecture? with practical examples, explanations, best practices, and real-world coding techniques.

Clean Architecture is a software architecture approach that organizes an application so that its core business rules remain independent from frameworks, databases, user interfaces, and external services. The architecture places business logic at the center and pushes technical implementation details toward the outside.

The main goal is to make the application easier to understand, test, and change. A database can be replaced, a web framework can change, or an external service can be swapped without forcing the core business rules to depend on those implementation details.

Clean Architecture is especially useful for applications with meaningful business rules and a long expected lifetime. It is less about creating a specific folder structure and more about controlling dependencies between different parts of the system.

The Core Idea Behind Clean Architecture

The central idea is to separate the application into layers with a clear dependency direction. The most important business rules should remain independent from outer technical concerns.

+------------------------------------------------------+
|                Frameworks & Drivers                  |
|                                                      |
|   +----------------------------------------------+   |
|   |          Interface Adapters                  |   |
|   |                                              |   |
|   |   +--------------------------------------+   |   |
|   |   |        Application / Use Cases      |   |   |
|   |   |                                    |   |   |
|   |   |   +----------------------------+   |   |   |
|   |   |   |      Enterprise Rules      |   |   |   |
|   |   |   |      / Domain Model       |   |   |   |
|   |   |   +----------------------------+   |   |   |
|   |   +--------------------------------------+   |
|   +----------------------------------------------+   |
+------------------------------------------------------+

Dependencies point toward the center.

The inner layers should not depend on the outer layers. Instead, abstractions can be defined closer to the application core and implemented by outer layers when infrastructure capabilities are required.

The Dependency Rule

The dependency rule is the most important principle of Clean Architecture: source-code dependencies should point inward.

For example, a domain entity should not directly depend on an ASP.NET Core controller, a database provider, or a specific messaging framework. Those technologies belong to the outer part of the application.

Frameworks
    |
    v
Adapters
    |
    v
Use Cases
    |
    v
Domain

Allowed:
Outer ---> Inner

Avoid:
Inner ---> Outer

This rule reduces the influence that implementation details have on the application's most valuable logic.

Main Layers of Clean Architecture

Different projects may use different names, but Clean Architecture is commonly described using a small set of conceptual layers.

Entities

Entities contain the most important business concepts and rules. They represent behavior that is central to the application's domain rather than details of a specific framework or database.

Use Cases

Use cases describe what the application does for users or external systems. They coordinate application-specific business operations without depending directly on infrastructure details.

Interface Adapters

Interface adapters convert data between the application's internal representation and external interfaces such as HTTP requests, database models, or presentation models.

Frameworks and Drivers

This outer layer contains technical details such as web frameworks, databases, message brokers, email providers, file storage, and other external systems.

Entities and Business Rules

Entities sit near the center because they represent business concepts that should remain meaningful even if the technology around them changes.

For example, an order might contain rules that determine whether it can be cancelled after completion.

public class Order
{
    public bool IsCompleted { get; private set; }

    public void MarkAsCompleted()
    {
        IsCompleted = true;
    }

    public void Cancel()
    {
        if (IsCompleted)
        {
            throw new InvalidOperationException(
                "A completed order cannot be cancelled.");
        }
    }
}

This rule does not need to know whether the order came from an HTTP request, PostgreSQL, SQL Server, or another external source.

Use Cases in Clean Architecture

A use case represents an application operation such as creating an order, registering a user, processing a payment, or submitting an assessment. It coordinates the steps required to perform that operation.

public class CreateOrderUseCase
{
    private readonly IOrderRepository _orders;

    public CreateOrderUseCase(IOrderRepository orders)
    {
        _orders = orders;
    }

    public async Task ExecuteAsync(Order order)
    {
        order.Validate();

        await _orders.AddAsync(order);
    }
}

The use case depends on an abstraction rather than directly creating a database connection or calling a specific database library.

Dependency Inversion in Clean Architecture

Dependency inversion allows inner application logic to define the abstractions it needs while the outer infrastructure provides concrete implementations.

For example, an application can define an order repository interface:

public interface IOrderRepository
{
    Task<Order?> GetByIdAsync(Guid id);
    Task AddAsync(Order order);
}

Infrastructure can then provide an implementation using a specific database technology.

public class OrderRepository : IOrderRepository
{
    private readonly AppDbContext _db;

    public OrderRepository(AppDbContext db)
    {
        _db = db;
    }

    public async Task<Order?> GetByIdAsync(Guid id)
    {
        return await _db.Orders.FindAsync(id);
    }

    public async Task AddAsync(Order order)
    {
        _db.Orders.Add(order);
        await _db.SaveChangesAsync();
    }
}

The application core knows about the abstraction, while the outer layer knows the technical implementation.

Example Clean Architecture Project Structure

A common ASP.NET Core implementation may separate the application into several projects:

MyApplication.Domain/
    Entities/
    ValueObjects/
    Interfaces/

MyApplication.Application/
    UseCases/
    Commands/
    Queries/
    DTOs/
    Interfaces/

MyApplication.Infrastructure/
    Persistence/
    Repositories/
    Messaging/
    ExternalServices/

MyApplication.Web/
    Controllers/
    Middleware/
    Configuration/

The exact folder and project names can vary. The important concern is keeping dependencies aligned with the architecture's intended direction.

A Request Through Clean Architecture

Consider an API request that creates an order.

HTTP Request
      |
      v
OrdersController
      |
      v
CreateOrderUseCase
      |
      v
Order Domain Rules
      |
      v
IOrderRepository
      |
      v
Infrastructure Repository
      |
      v
Database

The controller handles HTTP concerns, the use case coordinates the application operation, the domain enforces business rules, and the infrastructure handles persistence.

Each part has a focused responsibility, while the dependency direction protects the core application logic from infrastructure details.

Benefits of Clean Architecture

Framework Independence

Core business rules do not need to depend directly on a particular web framework or infrastructure technology.

Database Independence

Business logic can remain separate from a specific database implementation.

Testability

Important business behavior can be tested without requiring the entire infrastructure stack.

Easier Technology Changes

Technical implementations can change with less impact on the core business logic when dependencies are properly controlled.

Limitations of Clean Architecture

Clean Architecture introduces additional boundaries, interfaces, and abstractions. These can be valuable for complex systems but may add unnecessary structure to very small applications.

Limitation Explanation
More abstractions Interfaces, use cases, adapters, and boundaries introduce more concepts than a simple application may require.
More initial setup Developers need to establish project boundaries and dependency rules before implementing features.
Learning curve Teams need to understand dependency inversion, use cases, and architectural boundaries.
Overengineering risk Applying every abstraction to a simple CRUD application can make the code harder to navigate without providing proportional value.

Clean Architecture vs Layered Architecture

Both approaches separate responsibilities, but they place different emphasis on dependency direction.

Aspect Layered Architecture Clean Architecture
Main focus Separation of technical responsibilities Protecting the application core and controlling dependencies
Dependency direction Often follows layer order Explicitly points inward
Business logic May be placed in a business layer Intentionally protected from outer implementation details
Abstractions May or may not be central to the design Commonly used to invert dependencies

A real application can combine ideas from both approaches. For example, an application may use Clean Architecture's dependency rules while still organizing individual projects around familiar layers.

Clean Architecture vs Onion Architecture

Clean Architecture and Onion Architecture share several important principles, especially separation of concerns, dependency inversion, and keeping the business core independent from infrastructure.

The terminology and exact layer organization can differ between implementations, but both approaches emphasize protecting the core of the application from external technical concerns.

Shared Principle

Business rules should not become dependent on details such as the web framework, database engine, message broker, or external service provider.

Testing Clean Architecture

Because business logic is separated from infrastructure, developers can test important rules without requiring a complete production-like environment.

[Fact]
public void CancelOrder_ShouldThrow_WhenOrderIsCompleted()
{
    var order = new Order();

    order.MarkAsCompleted();

    Assert.Throws<InvalidOperationException>(
        () => order.Cancel());
}

The test focuses on the business rule rather than HTTP, database, or infrastructure behavior.

Common Mistakes

Putting Framework Dependencies in the Domain

Adding direct dependencies on web frameworks, ORMs, messaging libraries, or other infrastructure technologies weakens the independence of the application core.

Creating an Interface for Every Class

Interfaces should support meaningful dependency boundaries. Creating abstractions for every class can add complexity without providing a clear architectural benefit.

Treating Folders as the Architecture

A project can have Domain, Application, Infrastructure, and Web folders while still violating the dependency rule. The actual project references and source dependencies matter more than folder names.

Making Use Cases Too Large

A use case should coordinate a focused application operation. Turning one use-case class into a large collection of unrelated business rules creates a different kind of coupling.

Overengineering Small Applications

Not every small project requires multiple projects, abstractions, and architectural boundaries. The structure should match the complexity and expected lifetime of the system.

Best Practices for Clean Architecture

  1. Keep business rules independent from frameworks and infrastructure.
  2. Make dependencies point toward the application core.
  3. Define abstractions where they provide meaningful dependency boundaries.
  4. Keep infrastructure implementations outside the core business logic.
  5. Keep use cases focused on application-specific operations.
  6. Keep domain models focused on business concepts and rules.
  7. Test important business behavior independently from infrastructure where practical.
  8. Avoid introducing architectural abstractions without a clear reason.
  9. Review dependency direction as the application grows.

When Should You Use Clean Architecture?

Clean Architecture can be useful when the application contains significant business rules, is expected to evolve over time, or needs clear separation between business behavior and infrastructure.

  • Large or long-lived business applications.
  • Systems with complex domain rules.
  • Applications that require extensive automated testing.
  • Projects where infrastructure technologies may change.
  • Enterprise applications with multiple integrations.
  • Systems where maintainable dependency boundaries are important.

For a small application with simple CRUD operations, a lighter architecture may provide a more appropriate balance between structure and development speed.

Frequently Asked Questions

What is Clean Architecture?

Clean Architecture is an approach that separates business rules from external implementation details and organizes dependencies so that they point toward the application core.

What is the dependency rule in Clean Architecture?

The dependency rule states that source-code dependencies should point inward toward the core business and application rules.

Does Clean Architecture require multiple projects?

No. Multiple projects can help enforce boundaries, but the architecture is fundamentally about responsibilities and dependency direction.

Is Clean Architecture the same as Onion Architecture?

They are closely related approaches with several shared principles, including dependency inversion and protecting business logic from infrastructure details. Their terminology and implementation structure can differ.

Can Clean Architecture be used with ASP.NET Core?

Yes. ASP.NET Core can be placed in the outer part of the architecture while the domain and application logic remain independent from the web framework.

Is Clean Architecture suitable for every project?

No architecture is universally appropriate. Clean Architecture adds structure and abstraction, so the additional complexity should be justified by the application's requirements.

Interview Questions

  1. What is Clean Architecture?
  2. What is the Dependency Rule?
  3. What are the main layers of Clean Architecture?
  4. What is the role of entities in Clean Architecture?
  5. What are use cases and why are they important?
  6. How does dependency inversion work in Clean Architecture?
  7. Why should the domain layer not depend on a database or framework?
  8. What are the benefits of Clean Architecture?
  9. What are the disadvantages of Clean Architecture?
  10. What is the difference between Clean Architecture and Layered Architecture?
  11. What is the difference between Clean Architecture and Onion Architecture?
  12. When should Clean Architecture not be used?

Summary

Clean Architecture separates an application's core business and application rules from external technologies such as web frameworks, databases, messaging systems, and third-party services.

Its most important principle is the dependency rule: dependencies should point inward toward the application core. Use cases coordinate application behavior, domain entities represent important business concepts, adapters translate between internal and external representations, and infrastructure provides technical implementations.

The architecture can improve testability, maintainability, and the flexibility to change infrastructure. However, it also introduces additional abstractions and should be applied according to the complexity and requirements of the project.

Pro Tip

Do not measure Clean Architecture by the number of projects or interfaces. The real goal is protecting business rules from external implementation details and keeping dependency direction intentional.

More Software Architecture lessons

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.