ABOUT PORTFOLIO BLOG GALLERY RESOURCES CONTACT
Home / Blog / Software Engineering

Clean Architecture Explained with Real Project Example

Author

Rizal Azis

Author

Aug 03, 2026
12 Min Read
Clean Architecture Explained with Real Project Example

Learn Clean Architecture with real-world project examples. Discover its layers, principles, benefits, folder structure, and implementation in modern software development.

Clean Architecture Explained with Real Project Examples

As software projects grow, maintaining clean, scalable, and testable code becomes increasingly challenging. Many applications start as simple projects but eventually become difficult to modify because business logic, database operations, and user interface code are tightly coupled.

This is where Clean Architecture provides a structured solution.

Popularized by Robert C. Martin (Uncle Bob), Clean Architecture helps developers organize applications so that business rules remain independent of frameworks, databases, and external services. The result is software that is easier to maintain, test, and extend over time.

In this guide, you'll learn what Clean Architecture is, why it matters, how each layer works, and see practical examples based on real-world software projects.

What Is Clean Architecture?

Clean Architecture is a software architecture pattern that separates an application into independent layers, each with a clear responsibility.

The core principle is simple:

Business rules should not depend on frameworks, databases, or external technologies.

Instead of allowing infrastructure to dictate application design, Clean Architecture keeps the business logic at the center while everything else depends on it.

This separation makes software easier to adapt when technologies change.

Why Is Clean Architecture Important?

Without a proper architecture, applications often suffer from:

  • Large controllers containing business logic

  • Duplicate code across multiple modules

  • Difficult unit testing

  • Strong dependency on frameworks

  • Expensive maintenance as the project grows

Clean Architecture solves these problems by clearly separating responsibilities.

Benefits include:

  • Better maintainability

  • Easier testing

  • Framework independence

  • Improved scalability

  • Cleaner code organization

  • Faster onboarding for new developers

The Four Layers of Clean Architecture

The Four Layers of Clean Architecture

Clean Architecture is commonly represented as four concentric circles.

1. Entities (Enterprise Business Rules)

The innermost layer contains the core business rules.

Examples:

  • User

  • Product

  • Invoice

  • Order

  • Payment

Entities should contain only business logic and should never know anything about databases, APIs, or frameworks.

Example:

class Product

{

public function calculateDiscount()

{

...

}

}

2. Use Cases (Application Business Rules)

This layer contains the application's business processes.

Examples:

  • Create Order

  • Register User

  • Login

  • Checkout

  • Process Payment

A Use Case coordinates entities to accomplish a business goal.

Example:

Customer

Create Order

Validate Stock

Save Order

3. Interface Adapters

This layer connects business logic with external systems.

Examples:

  • Controllers

  • API Resources

  • Repositories

  • View Models

Responsibilities include:

  • Receiving HTTP requests

  • Converting data formats

  • Calling Use Cases

  • Returning responses

4. Frameworks & Drivers

The outermost layer contains infrastructure.

Examples include:

  • Laravel

  • Spring Boot

  • ASP.NET Core

  • Express.js

  • MySQL

  • PostgreSQL

  • Redis

  • AWS

These technologies can change without affecting the business logic.

The Dependency Rule

The most important principle in Clean Architecture is the Dependency Rule.

Dependencies always point inward.

Frameworks

Controllers

Use Cases

Entities

The core business rules never depend on outer layers.

This allows infrastructure to evolve without rewriting application logic.

Clean Architecture Folder Structure

Clean Architecture Folder Structure

A common backend project structure looks like this:

src/

├── Domain

│ ├── Entities

│ ├── ValueObjects

│ └── Interfaces

├── Application

│ ├── UseCases

│ ├── DTOs

│ └── Services

├── Infrastructure

│ ├── Database

│ ├── Repository

│ ├── Cache

│ └── ExternalAPI

├── Presentation

│ ├── Controllers

│ ├── Requests

│ └── Resources

└── Routes

This organization keeps business logic isolated from implementation details.

Real Project Example: E-Commerce Platform

Imagine you're building an online store.

Domain Layer

Contains:

  • Product

  • Customer

  • Order

  • Cart

  • Payment

These classes implement business rules such as stock validation or discount calculation.

Application Layer

Use Cases:

  • Create Order

  • Checkout

  • Add Product to Cart

  • Cancel Order

Each use case focuses on a single business operation.

Infrastructure Layer

Responsible for:

  • MySQL database

  • Payment gateway integration

  • Email service

  • Cloud storage

If you switch from MySQL to PostgreSQL, only this layer changes.

Presentation Layer

Handles:

  • REST API endpoints

  • Web controllers

  • Request validation

  • JSON responses

Controllers should remain thin and simply delegate work to the appropriate Use Case.

Real Project Example: Banking Application

A banking application might include:

Entities

  • Account

  • Transaction

  • Customer

  • Loan

Use Cases

  • Transfer Money

  • Open Account

  • Deposit Funds

  • Withdraw Funds

Infrastructure

  • Core Banking API

  • Payment Gateway

  • Fraud Detection

  • Database

Presentation

  • Mobile App API

  • Internet Banking

  • Admin Dashboard

Because business rules are isolated, introducing a new mobile application or integrating another payment provider requires minimal changes to the core logic.

Clean Architecture vs Layered Architecture

Feature

Clean Architecture

Layered Architecture

Dependency Direction

Inward

Often downward

Framework Independence

Excellent

Moderate

Testability

Excellent

Good

Business Logic Isolation

Strong

Moderate

Scalability

High

Moderate

Complexity

Higher

Lower

Layered Architecture is easier to learn, while Clean Architecture offers greater flexibility for large and long-lived applications.

Advantages of Clean Architecture

Easy Unit Testing

Business rules can be tested without databases or web servers.


Framework Independence

Switching frameworks should not require rewriting core business logic.


Better Maintainability

Responsibilities are clearly separated, making the codebase easier to navigate.


Improved Team Collaboration

Different teams can work independently on the Presentation, Application, and Infrastructure layers.


Long-Term Scalability

Applications remain organized as features and complexity increase.


Common Mistakes

Developers often make these mistakes when adopting Clean Architecture:

  • Putting business logic inside controllers

  • Using database models directly in the Domain layer

  • Creating unnecessary abstractions for small projects

  • Ignoring dependency inversion

  • Overengineering simple CRUD applications

Remember:

Clean Architecture is a tool for managing complexity—not a requirement for every project.

When Should You Use Clean Architecture?

Clean Architecture is a great choice when:

  • Building enterprise applications

  • Working with multiple developers

  • Creating long-term SaaS products

  • Developing financial or healthcare systems

  • Managing large APIs

  • Building systems that require extensive testing

For small prototypes or simple CRUD applications, a lightweight layered architecture may be sufficient.

Best Practices

When implementing Clean Architecture:

  • Keep controllers thin.

  • Place business rules inside Use Cases.

  • Protect the Domain layer from framework-specific code.

  • Follow SOLID principles.

  • Use dependency injection.

  • Write comprehensive unit tests.

  • Avoid unnecessary complexity.

Clean Architecture works best when applied pragmatically.

Conclusion

Clean Architecture is more than a folder structure—it's a way of designing software that keeps business logic independent from technology choices.

By organizing applications into well-defined layers, developers can build systems that are easier to test, maintain, and evolve over time.

Whether you're creating an e-commerce platform, a banking application, or a SaaS product, Clean Architecture provides a solid foundation for long-term success. Start with the core principles, apply them incrementally, and let your architecture grow alongside your application.


Frequently Asked Questions (FAQ)

What is Clean Architecture?

Clean Architecture is an architectural pattern that separates business logic from frameworks, databases, and user interfaces, making software easier to maintain and test.

Is Clean Architecture suitable for small projects?

Not always. For simple CRUD applications or prototypes, a basic layered architecture is often more practical. Clean Architecture becomes more valuable as complexity increases.

Is Clean Architecture the same as Layered Architecture?

No. While both separate responsibilities, Clean Architecture emphasizes dependency inversion and keeps the Domain layer independent of infrastructure.

Which frameworks support Clean Architecture?

Many modern frameworks support it, including Laravel, ASP.NET Core, Spring Boot, NestJS, Express.js, Django, and Flutter.

Related Articles

What Is Software Architecture? A Beginner's Guide (2026)

Software Engineering — 14 min read

Monolithic vs Microservices: Which Architecture Should You Choose in 2026?

Software Engineering — 15 min read