Reputation: 175
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!
Upvotes: 13
Views: 6290
Reputation: 1
Service is the a core business logic of your application, entire working flow and you can't provide the implementation from outside(from infrastructure layer). Only repositories should be implemented from the outside, because they are specific, for example you implemented DB connections with Ado.net, but one day you decided to change it with EF.core. You should create new class in infrastructure and implement the same repository interface from core. Erom executable project You can control which implementation to use.
Upvotes: 0
Reputation: 4738
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>();
Upvotes: 28