Both Clean Architecture and Onion Architecture follow the same core rule:
🔑 Dependencies always point inward toward the Domain.
Onion / Clean Architecture – Layer Overview
Typical layers (from inside to outside):
Example: Employee Management System
We’ll implement:
“Create a new Employee and save it.”
Domain Layer (Core Business Logic)
📁 Domain/
This is the center of the onion.
Where to define domain objects?
👉 HERE.
Domain objects like:
-
Employee
-
Order
-
Product
-
Customer
must live in Domain.
They must NOT depend on:
-
Database
-
Frameworks
-
Controllers
-
UI
Domain Entity
📁 Domain/Entities/Employee.cs
✔ Business rules live here
✔ No EF, no DB, no HTTP
Repository Interface (Contract)
📁 Domain/Interfaces/IEmployeeRepository.cs
IMPORTANT:
The interface lives in Domain, not Infrastructure.
Why?
Because Domain defines what it needs.
Infrastructure will implement it.
This keeps dependencies inward.
Application Layer (Use Cases)
📁 Application/
This layer orchestrates business operations.
Where to implement use cases?
👉 HERE.
Example use case:
-
CreateEmployee
-
IncreaseSalary
-
GetEmployee
Create Employee Use Case
📁 Application/UseCases/CreateEmployee.cs
✔ Uses Domain entity
✔ Uses Domain interface
✔ Does NOT know about database
Infrastructure Layer (External World)
📁 Infrastructure/
This is where:
-
EF Core
-
SQL
-
MongoDB
-
File system
-
External APIs
live.
Where to implement repository?
👉 HERE.
EF Repository Implementation
📁 Infrastructure/Repositories/EmployeeRepository.cs
✔ Implements Domain interface
✔ Depends on EF
✔ Domain does NOT depend on this
Dependency direction:
Correct ✅
Presentation Layer (API / UI)
📁 WebAPI/Controllers/EmployeeController.cs
Where to call the use case?
👉 HERE (Presentation Layer)
✔ Controller calls Use Case
✔ Controller does NOT create Employee directly
✔ Controller does NOT talk to DB
Summary – Clear Answers
❓ Where to define domain objects like Employee or Order?
Domain Layer
They contain:
-
Business rules
- Validation
- Core behavior
They must NOT know:
❓ Where to implement repository?
- Interface → Domain
- Implementation → Infrastructure
❓ Where to implement business logic?
- Core rules → Domain
- Use case orchestration → Application
❓ Where to call use cases?
From:
-
Controllers
- API endpoints
- UI
- Background jobs
That’s the Presentation Layer.
Dependency Rule (Most Important)
Only this direction is allowed:
🚫 Domain must never depend on outer layers.
Clean vs Onion?
Practically identical in structure.
-
Onion Architecture was popularized by Jeffrey Palermo
- Clean Architecture was formalized by Robert C. Martin
Both enforce:
“Business rules should not depend on frameworks.”