EventStore 订阅一个类别的流

EventStore Subscribing to a stream for a category

我已经开始在 .Net 中创建一个测试应用程序,它使用 Greg Young 的 EventStore 作为 CQRS/ES 的后备存储。

为了便于加载完整聚合,我将其保存到名称为 "agg-123" 的流中。例如,对于 ID 为 553 的产品聚合,将有一个名为 "product-553" 的流。然后对于 "Order" 聚合,流将被命名为 "order-123".

从事件的再水化和保存来看,这很有效。

我现在正在尝试创建一个侦听器,它将侦听某些流,然后填充查询数据库。我看到的订阅方法好像只能订阅"order-123",或者"all"。我看不出如何订阅 "product-" 或 "order-",或两者。

我想要么

有人有什么建议吗?

来自 The cost of creating a stream article:

Generally when people are wanting only a few streams its because they want to read things out in a certain way for a particular type of reader. This can be done in other ways. Internally the Event Store is essentialy a topic based pub/sub. What you can do is repartition your streams utilizing projections to help provide for a specific reader. As an example let's say that a reader was interested in all the InventoryItemCreated and InventoryItemDeactivated events but was not interested in all the other events in the system. Supporting this stream when we have the events in many millions of streams can still be done.

To do this we will create a projection to reindex the streams.

所以我们的想法是您可以创建两个投影来向某些 productsorders 流发出事件。

在同一篇文章中提到了系统按类型投影,它为每个事件类型创建一个名为 $et-{typename} 的流。这也可能对您的情况有用。

例如,如果您只对观察聚合的创建感兴趣,则可以订阅相应的流。假设您有一些 productCreatedorderCreated 事件,您只需订阅 $et-productCreated$et-orderCreated 事件。从这些流中接收到事件后,您还可以订阅各个流(例如 product-553order-123),只需使用 *Created 事件中的 Id 即可。

(注意:默认情况下禁用投影,启用后您可能需要手动启动按类型投影。)

在您的预测中,您可以使用 fromCategory()。 EventStore 按流的名称对每个流进行分类,直到第一个“-”。因此,您的 'order-123' 和 'order-456' 流都属于 'order' 类别。

所以你可以这样做:

fromCategory('order')
  .whenAny(function(s,e) {
    linkTo('orders',e);
  });

这会将所有订单聚合中的所有订单相关事件投射到您可以订阅的单个 'orders' 流中。

您需要确保类别预测为 运行。 (我已经有一段时间没有使用 EventStore 了,当投影处于测试阶段并且默认情况下未启用时它又回来了,不确定最新版本是否相同)

您需要检查投影是否 运行,因为在撰写本文时此功能默认处于禁用状态 post,因为投影功能仍处于测试阶段。

有一种特殊类型的投影称为 类别投影,您可能需要它。如果您有按照 name-id 模式命名的流,例如 product-123product-234,您可以订阅 $ce-product 流以接收保存到这些流中的所有事件.