Onion Architecture: Business Services Interfaces and Implementation

前端 未结 1 625
隐瞒了意图╮
隐瞒了意图╮ 2021-02-02 17:11

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:

  • M
相关标签:
1条回答
  • 2021-02-02 17:49

    The core layer should contain:

    • Models/Entities/POCOs/Whatever_the_name... it's all about domain objects
    • ALL the interfaces (including repositories and services)
    • Your core business services implementation (*)

    (*) 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>();
    
    0 讨论(0)
提交回复
热议问题