Onion Architecture In ASP.NET MVC

Many traditional architectures exist in the world of web. Each architecture has its own advantages and disadvantages. The main problem faced by these architectures is the separation of concerns and tight coupling. To overcome these problems, the concept of Onion Architecture was introduced in 2008 by Jeffery Palermo. The main aim of this architecture was to make applications loosely coupled and create proper separation of concerns in an ASP.NET application development India. It makes development, testing, and maintenance very easy.

When it is necessary for an entity/object to know about other objects, or they largely depend on interfaces where service methods are defined, it is called tight coupling. Whereas in loose coupling, the dependency between objects is very little and there is proper separation of concerns among interfaces. As a result, the framework is more stable and its maintainability is very easy.

ONION ARCHITECTURE OVERVIEW


As shown in the above image, the Domain Model is the core and it consists of POCO entries. The brown layer has service interfaces. The green layer is the layer of implementation or the Infrastructure layer. All the service methods, repositories, and dependency injections are implemented in this layer. The blue layer has User Interfaces and the testing.

No transitive dependency exists between the UI, Test, and the Data Access Layer that makes it better for Unit Testing of MVC applications. The implementation of application service is in separate layer and dependency is on Core domain.

USING THE CODE

The project structure will look as follows if Onion architecture is implemented.


The CORE

The Core looks like the following.



There is a class library project in the Core folder called as interfaces for both Services and Repositories. There is a Model with an auto generated file (.tt file) that contains POCO entities.

The INFRASTRUCTURE

This folder is composed of the integral part of the application. There are multiple projects in this folder. The first project is the class library project and there is .edmx file in its first folder named as data. It also contains the ".tt" file and the context classes.
 
The second project in this folder is dependency injection. In this project, there are two classes- each for the Service Module and the Repository module.
 
The third project in this folder is the Error logging project. It helps in identifying exceptions or errors in the database. In the infrastructure project, those operations are performed that are linked to the outside application, such as operations related to access of file systems, database operations, access of outside service. The following activities need to be performed while creating the Infrastructure project.
  1. Create a class for database context.
  2. Repository class implementation.
  3. Create a class for database initializer.

The WEB Project

Create an MVC application that will make use of the infrastructure project and the core project. Following references need to be added in the Web project:
  1. Add a reference to the infrastructure project.
  2. Add a reference to the core project.
  3. Add a reference to the package of Entity framework.

DEPENDENCIES



The Web project is dependent on other two projects i.e. the Infrastructure and the Core, as shown on the diagram above. Infrastructure project is dependent on the Core project and the Core Project is independent of every other project.

The main features of Onion Architecture include

  1. The main application is made as an independent model.
  2. Interface is defined by the inner layer while the implementation is done by the outer layer.
  3. As the name suggests, the direction of coupling is at the center.
An Onion Architecture, thus, helps to decouple the Business, the Infrastructure, and the User Interface, without using the concept of OOPS and the domain-driven concept.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now