首页 > 解决方案 > 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.

标签: elasticsearchspring-datadomain-driven-designspring-data-elasticsearch

解决方案


我认为你在正确的轨道上坚持无知明智。但是,当您只能查询 ES 时,利用 RDBMS + ElasticSearch 进行简单的搜索操作是一种资源浪费。

CQRS方法允许您区分需要针对 SQL 数据库启动事务以编写内容(命令)和仅读取 ElasticSearch(查询)的情况。


推荐阅读