本文介绍了如何在CQRS中处理基于集合的一致性验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的域模型,其中涉及一个Facility聚合根列表.鉴于我正在使用CQRS和事件总线来处理从域引发的事件,您如何处理集合上的验证?例如,说我有以下要求:

I have a fairly simple domain model involving a list of Facility aggregate roots. Given that I'm using CQRS and an event-bus to handle events raised from the domain, how could you handle validation on sets? For example, say I have the following requirement:

  1. Facility的名称必须唯一.
  1. Facility's must have a unique name.

由于我在查询端使用的是最终一致的数据库,因此在事件处理或处理事件时,不能保证其中的数据是准确的.

Since I'm using an eventually consistent database on the query side, the data in it is not guaranteed to be accurate at the time the event processesor processes the event.

例如,FacilityCreatedEvent在查询数据库事件处理队列中,等待处理并写入数据库.新的CreateFacilityCommand发送到要处理的域.域服务查询已读取的数据库,以查看是否已经使用该名称注册了其他Facility,但是由于CreateNewFacilityEvent尚未处理并写入到存储中,因此返回false.新的CreateFacilityCommand现在将成功并抛出另一个FacilityCreatedEvent,当事件处理器尝试将其写入数据库并发现已经存在该名称的另一个Facility时,该FacilityCreatedEvent将会崩溃.

For example, a FacilityCreatedEvent is in the query database event processing queue waiting to be processed and written into the database. A new CreateFacilityCommand is sent to the domain to be processed. The domain services query the read database to see if there are any other Facility's registered already with that name, but returns false because the CreateNewFacilityEvent has not yet been processed and written to the store. The new CreateFacilityCommand will now succeed and throw up another FacilityCreatedEvent which would blow up when the event processor tries to write it into the database and finds that another Facility already exists with that name.

推荐答案

我使用的解决方案是添加一个System聚合根,该根可以维护当前Facility名称的列表.创建新的Facility时,我将System聚合(仅一个System作为全局对象/单例)用作其工厂.如果给定的设施名称已经存在,则将引发验证错误.

The solution I went with was to add a System aggregate root that could maintain a list of the current Facility names. When creating a new Facility, I use the System aggregate (only one System as a global object / singleton) as a factory for it. If the given facility name already exists, then it will throw a validation error.

这将验证约束保留在域内,并且不依赖最终一致的查询存储.

This keeps the validation constraints within the domain and does not rely on the eventually consistent query store.

这篇关于如何在CQRS中处理基于集合的一致性验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 05:39