我以领域驱动设计的方式思考场景,其中我有实体,比如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,因为它将用于非状态更改的逻辑分离到基础结构中,但也有人担心,我们应该知道如何将搜索作为应用程序的重要部分。
发布于 2021-01-19 10:18:18
你现在面对的是写字和阅读模型。理想情况下,在写入模型中持久化您的实体/聚合之后,您应该分派该实体的未提交事件,并列出/订阅它们以生成投影(用例中为弹性的部分)。供参考:https://github.com/jorge07/symfony-5-es-cqrs-boilerplate/blob/symfony-5/src/Infrastructure/User/ReadModel/Projections/UserProjectionFactory.php#L17海事组织,实体不应包含存储库。
https://stackoverflow.com/questions/65771725
复制相似问题