I am learning about the Onion Architecture. I have a confusion about the service layer, because I see some people saying that the core layer should only contain:
But others express that it also should implement the services interfaces. So what layer should implement the services interfaces?
I thought that the infrastructure layer should implement:
And inject them to UI layer and Test layer when requested.
Thank you!
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.
Now we create the third layer of the onion architecture which is a service layer. To build this layer, we create one more class library project named OA. Service. This project holds interfaces and classes which have an implementation of interfaces. This layer is intended to build loosely coupled applications.
According to Jeffrey Palermo: "The overall philosophy of the Onion Architecture is to keep your business logic and model in the middle (Core) of your application and push your dependencies as far outward as possible.
The Onion architecture is a form of layered architecture and we can visualize these layers as concentric circles. Hence the name Onion architecture. The Onion architecture was first introduced by Jeffrey Palermo, to overcome the issues of the traditional N-layered architecture approach.
The core layer should contain:
(*) If your business is about handling orders, then the implementation of your IWorkOrderService
should be in the core layer. If your WorkOrderService
needs to access let's say a ShippingService
(which is not your business), then it will only manipulate the IShippingService
defined in the core layer and the IShippingService
implementation will be somewhere in the infrastructure layer.
If your WorkOrderService
needs an OrderRepository
it will be done excatly the same way.
Here's a code example:
namespace MyBusiness.Core.Services
{
internal class WorkOrderService: IWorkOrderService
{
public WorkOrderService(IOrderRepository orderRepository, IShippingService shippingService)
{
_orderRepository = orderRepository;
_shippingService = shippingService;
}
...
}
}
This will be up to the outermost layer of your onion architecture - the Dependency Resolution Layer - to bound all your interfaces with the right service implementation at run time.
For<IWorkOrderService>().Use<Core.Services.WorkOrderService>();
For<IShippingService>().Use<Infrastructure.Services.ShippingService>();
For<IOrderRepository>().Use<Infrastructure.Data.OrderRepository>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With