What is Clean Architecture / Onion Architecture?

SOLID & Design Patterns Architecture Advanced 3–5 Yrs5+ Yrs Architecture 1 min read

Quick Answer

Clean/Onion architecture organises code in concentric layers — Domain (entities, rules) at the centre, Application (use cases) around it, Infrastructure (EF, HTTP, files) on the outside. The dependency rule: source code dependencies only point inward. The domain knows nothing about the database or the web framework.

Clean Architecture puts the domain — the entities and business rules — at the centre, wraps the application layer with the use cases around it, and pushes all the infrastructure like EF and HTTP to the outer ring. The one rule is that dependencies only point inward: the domain has no reference to Entity Framework or ASP.NET. Inner layers declare interfaces like IOrderRepository and outer layers implement them, which is just Dependency Inversion applied at the architecture level. The payoff is that the business logic is testable with no database and you can replace the infrastructure. The cost is more projects and more mapping, so it's for a big, long-lived system.

  • Inner layers define interfaces (IOrderRepository); outer layers implement them — Dependency Inversion in action.
  • The web project and the EF project both depend on Application/Domain, never the reverse.
  • Benefits: the domain and use cases are testable without a database or web server; you can swap infrastructure.
  • Costs: more projects, more mapping, more indirection — justified for a large, long-lived app, heavy for a small one.
  • “Ports and adapters” (hexagonal) is the same idea framed as ports (interfaces) and adapters (implementations).