洋葱架构:业务服务接口和实现 [英] Onion Architecture: Business Services Interfaces and Implementation

查看:179
本文介绍了洋葱架构:业务服务接口和实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关洋葱架构。我有一个关于服务层的混乱,因为我看到一些人说,核心层应该只包含:

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:


  • 模式

  • 库接口

  • 服务接口

但也有人前preSS,它也应该实现服务接口。那么,什么层应实现服务接口?

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:


  • 库接口

  • 服务接口

和要求时,他们以注入UI层和测试层。

And inject them to UI layer and Test layer when requested.

感谢您!

推荐答案

核心层应包含:


  • 型号/实体/波苏斯/ Whatever_the_name ...它的所有有关的域对象

  • 所有的接口(包括库和服务)

  • 您的核心业务服务实施(*)

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

(*)如果你的业务是有关处理订单,那么执行你的 IWorkOrderService 应该是在核心层。如果你的 WorkOrderService 需要访问比方说,一个 ShippingService (这是不是你的业务),那么它只会操纵 IShippingService 在核心层定义, IShippingService 实施将是在基础设施层的某个地方。

(*) 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.

如果你的 WorkOrderService 需要一个 OrderRepository 将被excatly做了同样的方式。

If your WorkOrderService needs an OrderRepository it will be done excatly the same way.

下面是一个code例如:

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>();

这篇关于洋葱架构:业务服务接口和实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆