Understanding Onion Architecture: An Example Folder Structure

Alessandro Traversi
3 min readJun 23, 2023

--

Photo by cottonbro studio: https://www.pexels.com/photo/boy-in-white-t-shirt-sitting-on-chair-in-front-of-computer-4709285/

Onion Architecture is a software architectural pattern that promotes a modular and loosely coupled design, focusing on separation of concerns and maintainability. It helps developers create applications that are more flexible, testable, and easier to evolve over time. In this article, we will delve into the key concepts of Onion Architecture and provide an example folder structure that illustrates its implementation.

Key Concepts of Onion Architecture:

Dependency Rule:

Dependencies flow inward, with inner layers having no knowledge of outer layers. This ensures that high-level modules do not depend on low-level modules directly. Instead, both depend on abstractions, enabling interchangeable implementations and reducing coupling.

Layers of the Onion:

  • Domain Layer: Contains the core business logic, entities, and business rules of the application.
  • Application Layer: Implements use cases and coordinates the flow of data between the domain and infrastructure layers.
  • Infrastructure Layer: Handles external concerns such as databases, file systems, or external services.
  • Presentation Layer: Handles user interfaces and presentation-related logic.

Separation of Concerns:

Each layer has a distinct responsibility, ensuring that business logic remains decoupled from infrastructure or presentation concerns. This separation improves code maintainability and facilitates testing.

Example Folder Structure:

To demonstrate a common folder structure based on Onion Architecture, let’s consider a hypothetical e-commerce application.

Domain Layer:

Contains the core business logic and entities.

  • Example folders: src/domain/entities, src/domain/repositories, src/domain/services.

Application Layer:

  • Implements use cases and orchestrates the interaction between the domain and infrastructure layers.
  • Example folders: src/application/usecases, src/application/dtos, src/application/mappers.

Infrastructure Layer:

  • Handles external dependencies and technical details.
  • Example folders: src/infrastructure/persistence, src/infrastructure/services, src/infrastructure/external.

Presentation Layer:

  • Deals with user interfaces and presentation logic.
  • Example folders: src/presentation/controllers, src/presentation/views, src/presentation/routes.

By organizing the codebase according to this folder structure, developers can easily navigate and modify different components of the application. The folder structure promotes separation of concerns, with dependencies flowing inward, adhering to the dependency rule of Onion Architecture.

Benefits of Onion Architecture:

Modularity and Maintainability:

The clear separation of concerns and decoupling of dependencies enable easier maintenance and modification of code, making it more adaptable to changing requirements.

Testability:

Each layer can be independently tested, allowing for comprehensive unit tests and ensuring that business logic remains isolated from external dependencies.

Flexibility:

The modular design facilitates the introduction of new technologies or frameworks without affecting the core business logic, enhancing the scalability and future-proofing of the application.

Conclusion:

Onion Architecture provides a powerful approach to software development, emphasizing modularity, maintainability, and testability. By following the key principles and organizing the codebase into distinct layers, developers can create robust applications that are easier to understand, modify, and extend over time. The example folder structure presented in this article serves as a starting point for implementing Onion Architecture, with the flexibility to adapt it to the specific needs of each project.

--

--

Alessandro Traversi

Senior Fullstack Developer | NodeJS | VueJS | ReactJS at Hotmart