【发布时间】:2021-01-18 09:21:13
【问题描述】:
我正在以领域驱动设计的方式考虑场景,其中我有实体,比如说 Cv(简历),它的状态通过存储库保存在数据库中。
现在我需要将部分 Cv 存储在另一个系统 (ElasticSearch) 中,这对于搜索等整个应用功能至关重要。
如何处理?我正在考虑这两个选项:
1.使用域服务 IndexCvGatewayServiceInterface(作为基础设施中实现的接口)
class CvEntity
{
public function approve(CvRepositoryInterface $cvRepository, IndexCvGatewayServiceInterface $indexService)
{
$cvRepository->update($this);
$indexService->update($this);
}
}
2。监听域事件(创建基础架构监听器)
class CvEntity
{
public function approve(CvRepositoryInterface $cvRepository, EventDispatcheInterface $dispatcher)
{
$cvRepository->update($this);
$dispatcher->dispatch(new CvApprovedEvent($this));
}
}
我喜欢选项 2。因为它将用于非状态更改目的的逻辑分离到基础架构中,但也有人担心,我们应该知道搜索是我们应用程序的重要组成部分。
【问题讨论】:
标签: domain-driven-design domainservices domain-events