elasticsearch - How to integrate spring data elastic search with DDD principles?
问题描述
Problem:
Suppose the domain contains 3 aggregates and a search operation given a query string is executed towards a web application. The query string must be used to search all of the fields in each aggregate and return 3 sets of each aggregate to the user as the resulting query. The web application has two data stores: RDBMS and Elastic Search. How do we integrate the RDBMS+ES infrastructure to the domain layer?
Proposed Solution:
The best way I can think of is to maintain the persistence ignorance of the domain layer by hiding the RDBMS+ES infrastructure implementation behind each domain repository. Here is my proposed solution in pseudo-code format:
AggregateRepository
- jpaAggregateRepository
- elasticAggregateRepository
@Transactional
+ save(Aggregate) {
Denormalize Aggregate to AggregateDocument (because one aggregate can span many tables)
Persist Aggregate to jpaAggregateRepository
Persist AggregateDocument to elasticAggregateRepository
}
+ find() {
Extract PKs from elasticAggregateRepository.find()
return jpaAggregateRepository(PKs)
}
By doing so, I have successfully prevented any infrastructure logic to pollute my domain layer. To finally achieve the use case, I can execute the following in my application layer:
aggregateOneRepository.find(term)
aggregateTwoRepository.find(term)
aggregateThreeRepository.find(term)
and probably combine the 3 sets of aggregates into a DTO to be consumed by the client.
Is this solution dirty? If so, please do suggest a better solution.
解决方案
我认为你在正确的轨道上坚持无知明智。但是,当您只能查询 ES 时,利用 RDBMS + ElasticSearch 进行简单的搜索操作是一种资源浪费。
CQRS方法允许您区分需要针对 SQL 数据库启动事务以编写内容(命令)和仅读取 ElasticSearch(查询)的情况。
推荐阅读
- node.js - 后端容器无法连接到数据库 - Sequelize、Node、Postgres、Docker
- amp-html - amp-iframe 谷歌地方有时拒绝呈现
- c# - if 语句即使在错误条件下也会执行
- symfony - 使用映射的 false 和多个 true 验证 symfony 4 formfield 文件类型
- typescript - NestJS 微服务和云部署
- c# - 按下按钮时显示文本
- laravel - 当单击复选框时,将 laravel 中的一列更新为 1,将其他列更新为 0
- sql - 如何计算两个日期之间每周的时间量 - SQL Server
- vhdl - 指向指针的指针 - 旧指针是否仍然存在?
- vba - 仅在表格所在的位置更改方向页面