DEV Community

Narender Reddy
Narender Reddy

Posted on

πŸ§… Onion Architecture Explained Quickly

Modern software systems often become hard to maintain as they grow. Business logic gets mixed with UI, databases, and external services, making changes risky and expensive.
This is where Onion Architecture comes in.
It helps developers build applications that are clean, testable, scalable, and easier to maintain.


1) What is Onion Architecture?

Onion Architecture is a software design pattern that organizes your application into layers, like an onion πŸ§….
The core business logic stays at the center, while external concerns such as databases, APIs, and UI remain in outer layers.
Core Goal
β€’ Separation of concerns
β€’ Loose coupling
β€’ Dependency inversion
β€’ Clean maintainable code

The core idea:
Keep business rules independent from:
β€’ Database
β€’ UI
β€’ Frameworks
β€’ External APIs
That means your business logic can survive even if technology changes.


2.🧱 Layers of Onion Architecture

  1. Domain Layer (Core)
  2. Application Layer
  3. Infrastructure Layer
  4. Presentation Layer (API/UI) And in pictorial representation as below.

a. 🧠 Domain Model (Core)

This is the heart of the application.
Contains:
β€’ Business entities (e.g., Order, Customer)
β€’ Core business rules
β€’ Domain logic
β€’ Interfaces
β€’ Value Objects
β€’ Business Rules
βœ” No dependency on database, UI, or frameworks


b. πŸ“¦ Application Services Layer
Acts as a bridge between UI and domain.
Responsibilities:
β€’ Use cases (business workflows)
β€’ Coordinating domain objects
β€’ Calling repositories
Contains

  1. Services
  2. DTOs
  3. Interfaces
  4. Use Cases _________________________________

c. 🌐 Infrastructure Layer
Handles external concerns:
β€’ Database
β€’ APIs
β€’ File systems
Contains:

  1. Entity Framework Core
  2. Repository Implementations
  3. External Services

d. πŸ–₯️ Presentation Layer (UI)
This is what users interact with:
β€’ Web APIs
β€’ MVC Controllers
β€’ Frontend apps
β€’ Contains:

  1. ASP.NET Core Web API
  2. Controllers
  3. UI

3.πŸ”„ Dependency Rule (Golden Rule)

The golden rule of Onion Architecture:
Dependencies always point inward
β€’ Outer layers depend on inner layers
β€’ Inner layers NEVER depend on outer layers

Presentation β†’ Infrastructure β†’ Application β†’ Domain

4.πŸ’‘ Why Use Onion Architecture?

βœ… Benefits

  1. Separation of Concerns β€’ Business logic is clean and independent
  2. Testability β€’ You can test domain logic without database or UI
  3. Flexibility β€’ Easily swap database, UI, or frameworks
  4. Maintainability β€’ Changes in one layer don’t break others ________________________________________

5.🏁 When Should You Use It?

Use Onion Architecture when:
β€’ Building enterprise applications
β€’ Long-term maintainability is important
β€’ Multiple teams work on the system
Avoid it when:
β€’ Small/simple apps
β€’ Quick prototypes


6. Solution Structure:

Sample source code @https://github.com/nrboyapally/OnionArchitecture

7. πŸ› οΈ Common Design Patterns Used

β€’ Dependency Inversion Principle (DIP):The foundation of Onion Architecture. It ensures that high-level modules (the Core) do not depend on low-level modules (Infrastructure) . Instead, both depend on abstractions (interfaces) defined in the inner layers.
β€’ Dependency Injection (DI): Used to provide concrete implementations of interfaces at runtime. In C#, this is typically configured in the Startup.cs or Program.cs file to inject services and repositories into controllers.
Example:
o builder.Services.AddScoped();
o builder.Services.AddScoped();
β€’ Repository Pattern: Creates an abstraction between the domain and the data access layer. It treats data as an in-memory collection, allowing the business logic to remain agnostic of the underlying database technology.
Example:
public interface IProductRepository
{
Task> GetAll();
}
β€’ Service Pattern: Organizes business logic into distinct services. A Service Layer sits between the UI and Repository layers , holding business rules and coordinating tasks.

Domain & Data Patterns

β€’ Domain-Driven Design (DDD) Patterns: Onion Architecture is heavily influenced by DDD. Key patterns include Entities (objects with unique identity), Value Objects (immutable objects without identity), and Domain Services

β€’ Data Transfer Object (DTO): Used to pass data between layers (e.g., from the Application layer to the Presentation layer) without exposing internal domain entities directly to the external world.
β€’ Unit of Work: Often used alongside the Repository pattern to group multiple data operations into a single transaction, ensuring data integrity across different repositories.

Advanced & Behavioural Patterns
β€’ CQRS (Command Query Responsibility Segregation): Frequently combined with Onion Architecture to separate read operations (Queries) from write operations (Commands), often using the Mediator Pattern (via libraries like MediatR).
β€’ Abstract Factory Pattern: Sometimes used to dynamically create instances of repositories or services, further decoupling the application from specific implementations.
β€’ Adapter Pattern: The infrastructure layer essentially acts as a set of adapters that convert external technical details (like SQL or Web APIs) into formats the internal Core can understand.

Top comments (0)