【发布时间】:2021-02-22 08:20:49
【问题描述】:
我正在使用 PHP API 尝试遵循清洁架构模式,以便将来能够将应用程序的模块提取到微服务中。
我的问题是应用程序服务应该如何在不耦合的情况下相互使用。 即使我正在注入绑定的抽象(接口),注入服务的方法也在处理主机服务域之外的实体。所以将来我会耦合服务,我将无法将它们外部化。
<?php
/* Domain: INJECTED */
class InjectedService implements InjectedServiceInterface
{
public function get(int $id): InjectedServiceDomainEntity
{
return $this->repo->findById($id);
}
}
/* Domain: HOST */
class HostService implements HostServiceInterface
{
/** @var InjectedServiceInterface $injectedService */
private $injectedService;
public function __construct(InjectedServiceInterface $injectedService)
{
$this->injectedService = $injectedService;
}
public function someMethod($someId)
{
/** @var InjectedServiceDomainEntity $injectedServiceEntity */
$injectedServiceEntity = $this->injectedService->get($someId);
// here I'm managing an outsider entity
}
}
someMethod 我没有耦合服务吗?管理来自另一个服务/域的实体?
当我想将HostService 移动到微服务时会发生什么?
非常感谢您的想法。
【问题讨论】:
-
你确定这2个服务是应用服务吗?您提供的注入服务的代码可以是域服务,或者如果您只需要获取实体,您可以只注入 repo
-
@MohamedBouallegue 是的,分别是
OrderService和PaymentService。一个需要另一个。问题更像是这是 DTO 的地方,或者这些只是在层之间传递数据
标签: dependency-injection domain-driven-design clean-architecture decoupling coupling