简体   繁体   English

如何批量处理聚合并在DDD中仅发布一个事件?

[英]How to process aggregates in batch and publish only one event in DDD?

I run into a situation while developing my domain I hope someone can help me solve. 我在发展自己的领域时遇到了麻烦,希望有人能帮助我解决。

In my current project I need to read from a cache all the stored aggregates of one type (let's say ClientAggregate). 在我当前的项目中,我需要从缓存中读取所有存储的一种类型的聚合(比如说ClientAggregate)。 I read the aggregates from the service layer and then, for each one of them, I acquire a lock for the current aggregate to do some processing and then save it back. 我从服务层读取了聚合,然后对于它们中的每一个,我都获取了一个用于当前聚合的锁,以进行一些处理,然后将其保存回去。 The business logic requires that only one event is sent out with information about the modified aggregates because the system listening to this event is a legacy system and has a schema already defined. 业务逻辑要求仅发出一个事件,其中包含有关修改后的聚合的信息,因为侦听此事件的系统是旧系统,并且已经定义了一个模式。

The problem I have is that, because of this locking process, the event I send is sent from the service layer instead of the domain. 我的问题是,由于此锁定过程,我发送的事件是从服务层而不是域发送的。 The logic requires to send information of the aggregates that had been modified all together in one event. 该逻辑要求在一个事件中一起发送已全部修改的聚合的信息。 Because I need to lock aggregates individually for performance reasons, I cannot pass all the aggregates to a domain service to handle the logic from there and generate the event. 由于出于性能原因需要分别锁定聚合,因此无法将所有聚合传递给域服务以从那里处理逻辑并生成事件。 My option here would be to lock directly from inside the domain service but then I'd leak infrastructure logic into my domain. 我在这里的选择是直接从域服务内部进行锁定,但随后我会将基础结构逻辑泄漏到我的域中。

So, in my current solution, the service contains domain logic to iterate through the aggregates and generate the event. 因此,在我当前的解决方案中,该服务包含域逻辑以遍历聚合并生成事件。 Doing it inside a domain service, the logic would be in the domain but the locking mechanism would need to be moved along. 在域服务内部进行操作时,逻辑将在域中,但是锁定机制将需要一起移动。 A third option would be to publish an event for each modified aggregate but then there should be something able to consume them all, waiting somehow for the whole process to finish, so I don't know if this is even an option. 第三种选择是为每个修改后的聚合发布一个事件,但随后应该有一些东西可以消耗掉它们,以某种方式等待整个过程完成,所以我不知道这是否是一个选择。

The business logic requires that only one event is sent out with information about the modified aggregates because the system listening to this event is a legacy system and has a schema already defined. 业务逻辑要求仅发出一个事件,其中包含有关修改后的聚合的信息,因为侦听此事件的系统是旧系统,并且已经定义了一个模式。

That doesn't sound like the event is a real domain event to me. 对于我来说,这听起来好像不是一个真正的领域性事件。 If it was, the reason you are trying to build your system this way would be because the domain works like that and not "because the system listening to this event is a legacy system". 如果是这样,那么您尝试以这种方式构建系统的原因是因为域按这种方式工作,而不是“因为侦听此事件的系统是旧系统”。

So what you should do is this: 因此,您应该这样做:

  • Control the whole batch processing from the service layer as you already have it. 已有的服务层控制整个批处理。
  • If the logic that processes each aggregate is domain logic, extract that into a domain service and call it from the service layer. 如果处理每个聚合的逻辑是域逻辑,则将其提取到域服务中,然后从服务层调用它。
  • That domain service may also publish domain events, if the domain requires it . 如果域需要 ,该域服务也可以发布域事件。
  • Publish the "finished* event from the service layer when finished. This is not a domain event. 完成后从服务层发布“ finished *”事件。这不是域事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM