带有 Spring 的 DDD Java - 存储库返回 Mono/Flux [英] DDD Java with Spring - Repository returning Mono/Flux

查看:109
本文介绍了带有 Spring 的 DDD Java - 存储库返回 Mono/Flux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在我使用 Java 和 Spring Boot 实现的 DDD 项目中实现反应式 Mongo 存储库时遇到的一个问题.假设我们有这样的包结构:

I'm wondering about one issue encountered when implementing reactive Mongo repository in a DDD project which I implement using Java and Spring Boot. Suppose we have such package structure:

/app 
  |
  |------/application
  |        | 
  |        |------/order
  |                 |
  |                 |------OrderApplicationService.java
  |
  |------/domain
  |        |
  |        |------/order
  |                 |
  |                 |------Order.java
  |                 |------OrderRepository.java 
  |
  |------/infrastructure
           |
           |------/mongo
                    |
                    |------MongoOrderRepository.java

我的 OrderRepository.java 我想要一个方法来保存我的订单:

I my OrderRepository.java I want to have a method to save my Order:

public interface OrderRepository {

    Order save(Order order);
}

并在我的应用程序服务中使用它:

And use it in my application service:

@Service
public class OrderApplicationService {

    private final OrderRepository orderRepository;

    public OrderApplicationService(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }

    public void createOrder() {
        //Dumb order creation
        Order createdOrder = clientRepository.save(new Order());
    }
}

接下来我想编写实现 OrderRepository 的 MongoOrderRepository.假设我将使用 ReactiveMongoTemplate.问题是它的所有方法都返回 Flux 或 Mono,所以我无法从 OrderRepository 接口实现我的方法.

Next I want to write MongoOrderRepository which implements OrderRepository. Suppose I will use ReactiveMongoTemplate. The problem is that all its methods return Flux or Mono, so I can't implement my method from OrderRepository interface.

我看到的可能解决方案是:

Possible solutions I see are:

  1. 使 OrderRepository 中的 'save' 方法返回一个由单核细胞增多症.这种方法会用 Reactor 污染领域层特定类型并打破规则,说域层应该是框架代码免费.
  2. 开发某种包装层,但这会产生一些样板代码.
  3. 将 OrderService.java 移至基础设施层,但这也打破了一些基本的 DDD 概念.

有人看到更好的解决方案吗?

Does someone see any better solutions?

推荐答案

存储库应该与框架代码无关,这是真的,这是一个很好的做法,但您也需要务实,我有一个使用 java 的存储库lambda,这是一个可以争论的语言级框架.

The repository should be agnostic of framework code, that's true and that's a good practice, but you need to be pragmatic as well, I had repository where I used java lambda, which are language level framework one could argue.

使用 Flux 或 Mono 有什么好处,将它们作为界面的一部分进行宣传有什么好处?如果没有,您可以将实现细节保留在存储库实现中,并使接口没有反应性对象.

What is the benefit of using Flux or Mono, and what is the benefit of advertising them as part of the interface ? If there is none, you can then keep the implementation detail into the repository implementation and keep the interface free of reactive objects.

但是,如果这需要跨越应用层到端口适配器,那么我认为将它们放入存储库的接口定义中没有任何问题.

However, if this needs to span through the application layer to the port adapters, then I don't see any issues putting them in the interface definition of your repository.

话虽如此,您可能想要检查另一种方法,使用 CQRS 和六边形架构,您可以两全其美:

That being said, you might want to check another approach, with CQRS and Hexagonal Architecture, you can have best of both worlds:

  • 为命令(更新和创建部分)提供一个干净的存储库界面
  • 使用查询服务(纯 POJO,如果您使用的是 Java,在您的应用程序包中定义)为查询返回 Mono 或 Flux(阅读部分)

  • Having a clean repository interface for the command (Update and Create part)
  • Using a Query Service (plain POJO if you are in java, defined in your application package) returning a Mono or Flux for the Query (Read part)

OrderApplicationService.java(这里是通过命令创建更新删除)OrderQueryService.java(这里是阅读部分)

OrderApplicationService.java (here go the Create Update Delete via commands) OrderQueryService.java (here go the Read part)

您的应用程序服务包含对 OrderRepository 的引用,查询服务不使用存储库,因为它更直接地查询数据库,例如通过 ReactiveMongoTemplate.

Your application service contains a reference to your OrderRepository, the query service does not use the repository as it query more directly the db, via ReactiveMongoTemplate for instance.

在我的查询服务中,例如我在存储库实现中使用 Hibernate 时使用了普通的 JDBC 模板.

In my Query services I used plain JDBC Templates for instance while using Hibernate in the repository implementation.

这篇关于带有 Spring 的 DDD Java - 存储库返回 Mono/Flux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆