Content
Onion architecture consists of several concentric layers interacting with each other towards the core, which is the domain. The architecture does not depend on the data layer, as in a traditional three-tier architecture; it depends on real domain models. The popularity of microservices is growing due to the range of benefits they offer to developers and businesses. In this article, I will tell you about my experience of using onion architecture with a harmonized combination of DDD, ASP.NET Core Web API and CQRS for building microservices. Using the onion architecture our application is loosely coupled because our layers communicate with each other using the interface.
So, it’s important to have an architecture in which you can swap the technology without mess up all around the application. To keep an application a long-life it’s important to have business logic and infrastructure service concerns independent from business logic. Onion Architecture is based on the inversion of control principle.
Repository Layer
In the Application layer, the FareRepository is able to retrieve data from external sources and transform it into meaningful Business Entities. We will use the business domain of a Ride Sharing/Taxi Booking Application. It’s quite straight-forward, just think of Uber or Lyft.
Aliaksandr is a Senior .NET developer at SaM Solutions with 13 years of experience. Being a Microsoft certified engineer, he specializes in web development and has experience in creating desktop and mobile solutions. Aliaksandr is fond of learning new technologies, conducting meetups and teaching newbies at internal company courses. The presentation layer is our final layer that presents the data to the front-end user on every HTTP request.
Highly scalable, fully testable, easy to evolve, easy to adapt to new requirements, etc etc etc. I hope you’ll find my experience useful for your projects. You can check my github repository for technical details. To put it simply, every action in Web API is either a request or a command , but it shouldn’t do both. Consequently, each API method is divided into requests and commands. The main issues we faced were related to maintaining the low connectivity of microservices.
Briefly about Microservices
In general, the further in you go, the higher level the software becomes. This approach makes it possible to create a universal business logic that is not tied to anything. Based on the DDD model, we’ve created onion architecture . Bounded context — each microservice is built around some business function and uses bounded context as a design pattern.
onion architecture is comprised of multiple concentric layers interfacing each other towards the core that represents the domain. The architecture does not depend on the data layer as in classic multi-tier architectures, but on the actual domain models. This architecture follows the Dependency Inversion Principle as well as Domain-Driven Design DDD. Data model and business logic at the center, instead of infrastructure concerns .
To organize business logic for our project, we used Domain-Driven Design . Now we can see when we hit the GetAllStudent Endpoint we can see the data of students from the database in the form of JSON projects. For every service, we will write the CRUD operation using our generic repository. The external layer never depends on the external layer. A complete implementation would be provided to the application at run time. It provides us with better testability for unit tests, we can write the separate test cases in layers without affecting the other module in the application.
Benefits of Onion Architecture
The main problem with this architecture is that all layers are built on top of the Data Access Layer and are, in fact, tied to a certain type of data storage. If this type changes, it causes changes at all levels. The Entity Framework partially solves this problem, but it supports a limited number of database types. Bounded context is a good fit for a microservices architecture.
- These four projects represent four layers of the onion architecture.
- This layer is used to communicate with the presentation and repository layer.
- Another big advantage, it’s very easy to write automated tests for everything in the Application Core layer, because the infrastructure is outside that layer.
- We will follow the same project as we did for the Domain layer.
- The UserController has an action method named DeleteUser, which returns view to delete a user.
One of the most important parts of each application is architecture. A good genetic and strong skeleton are keys to success. But do not forget that the code should always be useful, not just cool in terms of architecture, etc. This layer can reference any project from the inner layer.
CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered. The core consists of the domain layer, repositories layer and services layer. The number of layers in the application core will vary, however, the domain will always be the very center of the onion architecture.
UI Layer
The Controller is used to handle the web request by action methods and returns View accordingly. Hence, it solves the problem of separation of concern while the Controller is still used to database access logic. In essence, MVC solves the separation of concern issue but the tight coupling issue still remains. This layer creates an abstraction between the domain entities and business logic of an application.
The user data is displayed in a tabular format and on this view, it has linked to add a new user, edit a user and delete a user. Now, we create a UserProfile entity, which inherits from the BaseEntity class. https://globalcloudteam.com/ The code snippet, mentioned below is for the UserProfile entity. Now, we create an User entity, which is inherited from BaseEntity class. The code snippet, mentioned below is for the User entity.
Onion architecture provides us with the batter maintainability of code because code depends on layers. In fact your business rules simply don’t know anything at all about the outside world. The code, which is specific for the platform, we’ll move to the Infrastructure and UI. These are the 4 base levels of the Onion architecture.
What is Onion architecture?
Our fare calculation depends on external services such as routing information and fare models. Interfaces for these are defined in the Domain Services layer — IFareRepostory and IRouteService. RiderFareCalculator is implemented in this layer also, and it depends on the fare repository and route service interfaces declared in the same layer. Note that with this approach, we do not depend on the external service, rather the external service depends on our declared contracts. Now we need to add a new project to our solution that will be the service layer. Today, most Agile methodologies are encouraging rapid development to implement or enhance features as quickly as possible.
How to Migrate On-premise SQL Database to Azure
In this layer, we typically add interfaces that provide object saving and retrieving behavior typically by involving a database. This layer consists of the data access pattern, which is a more loosely coupled approach to data access. These interfaces communicate between UI and data access logic.
This layer has an implementation of the Dependency Inversion Principle so that the application builds a loosely coupled application. On the other hand, Onion Architecture addresses both the separation of concern and tight coupling issues. As per traditional architecture, the UI layer interacts to business logic, and business logic talks to the data layer, and all the layers are mixed up and depend heavily on each other. In 3-tier and n-tier architectures, none of the layers are independent; this fact raises a separation of concerns.
The code snippet, mentioned below is the BaseEntity class. In this article, we have implemented the Onion architecture using the Entity Framework and Code First approach. We have now the knowledge of how the layer communicates with each other’s in onion architecture and how we can write the Generic code for the Interface repository and services. Now we can develop our project using onion architecture for API Development OR MVC Core Based projects.
It builds loosely coupled applications with in-built dependency injection in ASP.NET Core. This layer lies in the center of the architecture where we have application entities which are the application model classes or database model classes. Using the code first approach in the application development using Asp.net core these entities are used to create the tables in the database. In union architecture the model lies at center, then repository build upon it, and then service based on repositories, and then Presenters, APIs and testers on top of service layer. The DbContext must have an instance of DbContextOptions in order to execute. We will use dependency injection, so we pass options via constructor dependency injection.