Onion Architecture Vs Clean Architecture

In this article, we are going to learn about Onion Architecture vs Clean Architecture. These architectures play a key role when we build large and complex projects which require regular enhancements and extensions.
Here, we will understand Onion architecture and Clean architecture in detail. Also, we will see a basic example of a .Net Core-based application to understand onion and clean architecture.
But, before we proceed, we should understand what was the concern with prior architecture i.e. N-Layer Architecture.

Let’s be clear that Onion Architecture is one of the forms of Clean Architecture.

One more point, this article is very important from an interview perspective.

Overview of N-Layer Architecture

It is one of the most popular Architectures in ASP.NET Core Applications. In this architecture, the Presentation Layer generally holds the part that the User can collaborate with, i.e., WebApi, MVC, Webforms, etc. Business Logic is likely the main piece of this whole arrangement.

It holds all the rationale connected with the Business prerequisites. Presently, every application in a perfect world has its own committed Database. To access the Database, we present a Data Access Layer. This layer normally holds ORMs for ASP.NET to read or write to a database.

Disadvantages of N-Layer Architecture

To clearly understand the advantages of Onion Architecture in ASP.NET Core Applications, first, we need to learn the problems with N-Layer Architecture. It is one of the most commonly used Solution Architectures amongst .NET developers.

The N-Layer architecture was introduced to make sure that developers can work on their core expertise layers. For eg – a front-end developer can work on presentation layers and back-end developers can work on business layer and data access layers.

Although, it is good for small-scale applications. But not good for large and complex software applications. Also, if we will work with Microservice architecture it will get more complex to work with N-Layer architecture, this is one of the major issues with N-tier architecture.

One other problem with n-tier architecture is its nature to create tightly coupled layers and this may lead to deployment complexity because tight coupling is not a good practice.

Read: Dependency Injection to build loosely coupled component

Clean Architecture

Clean Architecture was introduced by Robert “Uncle Bob”. It has multiple forms and some of them are Hexagonal Architecture, Ports and Adapters, and Onion Architecture. It builds on the concepts of Onion Architecture with some refinement. Instead of “Domain Model”, it refers to the core as “Entities”.

So, the main purpose of Clean Architecture is to allow the business to adapt to changing technology and interfaces.

Now, we will see Onion architecture in detail.

Onion Architecture

The Onion architecture was first introduced by Jeffrey Palermo, to overcome the issues of the traditional N-layered architecture approach which we discussed above. It is based on the domain-driven model.

The onion layer solves the problem of tight coupling that we faced with n-tier architecture. This architecture has a Domain Layer, Repository Layer, Service Layer and Presentation Layer. However, Domain Layer will be at the core of the architecture.

Onion Architecture Layers
Onion Architecture Layers

Layers in Onion Architecture

Onion Architecture divides the complete projects into 4 different layers.

Domain Layer

It is the core of Onion architecture. This layer contains application entities and DB model classes which work independently.

So, the domain layer consists of the core business logic and rules of the application.

Now, let’s demonstrate the Domain Layer with a simple example in C#. Suppose we’re building a Product Review System.

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int Stock { get; set; }
    public List<ProductReview> Reviews { get; set; } = new List<ProductReview>();
}
public class ProductReview
{
    public int Id { get; set; }
    public int ProductId { get; set; }
    public string Review { get; set; }
}

Here, we have two main entities Product and ProductReview which represents products and reviews given on products.

Contracts in the domain layer:

public interface IProductRepository
{
    Product GetProductById(int id);
    void AddProduct(Product product);
}

public interface IProductReviewRepository
{
    ProductReview GetReviewByProductId(int id);
    void AddReview(ProductReview review);
}

Repository Layer

This layer works as a middle layer between Domain and Service layers. Repository Layer creates an abstraction between Domain Layer and Business Layer.

Service Layer

The service layer is used to hold the business logic of the application and also to set communication between the presentation and repository layers.

Presentation Layer

This layer presents the UI of the application. It communicates with other layers via interfaces.

Advantages of Onion Architecture in ASP.NET Core

The below factors show the major advantages of using Onion Architecture

  • Better Testability – As the layers are more independent, it is easy to write test cases for each of the layers.
  • Loosely Coupled – Onion architecture helps to write loosely coupled code which helps software developers to provide better maintainability.
  • Good for Microservice
  • Better flexibility, portability

Apart from that, it provides much cleaner code with well-structured Projects for better understanding with teams.

Onion Architecture Project Structure

Firstly, we will create one small project in Visual Studio and this will explain the structure of Onion Architecture.

Here, We have a Domain layer or Core layer, a Repository or infrastructure layer, a Service layer and then a Presentation or UI layer.

Secondly, we need to add the reference of Domain Layer in the repository or other projects:

Differences between Onion and Clean Architecture

As we know Onion Architecture is one of the forms of Clean Architecture. Both the architecture focus on layering of the applications. In Onion Architecture, the application reduces the dependencies on the outer layer. IoC (Inversion of Control) is one of the principles to implement Onion Architecture.

Clean Architecture also helps programmers to develop a testable, scalable and maintainable system structure. Similar to Onion architecture, Clean Architecture also divides the system into layers and each layer has its tasks and responsibilities.

Onion ArchitectureClean Architecture
This architecture includes:
Domain entities
Repository interfaces
Service interfaces
Infrastructure, UI
This includes:
Entities
Controllers and Gateways
DataBase, UI, External interfaces

Conclusion

Onion and Clean Architecture all centre around the idea of layering and providing scalable architecture instead of building tightly coupled modules. They build on each other and use different terminology.

We see the project structure in .NET Core to achieve Onion Architecture.

You may read this article - What is Data Science?

1 thought on “Onion Architecture Vs Clean Architecture”

Leave a Comment

RSS
YouTube
YouTube
Instagram