Onion Architecture - E-commerce Application
1. Domain Layer (Core of the Onion)
This layer contains the business rules and domain entities. It's the most important layer because it doesn't depend on any other layer.
Entities represent the core objects of the application, such as Customer, Product, and Order.
Customer Entity
Product Entity
Order Entity
2. Application Layer (Use Cases Layer)
This layer contains the application logic that orchestrates the interaction between domain entities to perform use cases.
In our example, a use case could be placing an order.
PlaceOrderUseCase
3. Infrastructure Layer (Interface Layer)
The infrastructure layer interacts with external systems like databases, file systems, third-party services, etc. It implements the interfaces defined in the application layer.
Repositories
Repositories handle data persistence. Below is an implementation of the IOrderRepository interface from the application layer.
Order Repository Interface (Application Layer).
Order Repository Implementation (Infrastructure Layer).
Customer Repository Interface (Application Layer).
Customer Repository Implementation (Infrastructure Layer).
4. Presentation Layer (UI Layer)
This is the user interface layer where interactions happen. It communicates with the application layer to perform actions like placing an order.
OrderController
Order Confirmation
- Order ID: @Model.Id
- Total Amount: @Model.TotalAmount
How the Layers Interact?
The layers communicate as follows.
- UI Layer (Controller): Receives the user input (e.g., clicking "Place Order") and calls the PlaceOrderUseCase.
- Application Layer (Use Case): Orchestrates business logic (e.g., calculating total, checking for discounts, saving order).
- Infrastructure Layer (Repository): This handles data persistence (e.g., saving the order to the database).
- Domain Layer (Entities): Contains business logic (e.g., validating discounts, calculating order total).