我已经关注 Event Store 一段时间了,困扰我的一件事是何时应该考虑编写投影?创建扁平投影是常见的做法吗?
The setup here is using a stream per aggregate
例如,假设我有 2 个如下所示的事件:
public class OrderPlaced
{
public OrderPlaced(string reference, Currency currency, Amount amount)
{
...
}
public string Reference { get; }
public Currency Currency { get; } //Custom type
public Amount Amount { get; } //Custom type
}
public class OrderCompleted
{
public OrderCompleted(string reference)
{
this.Reference = reference;
}
public string Reference { get; }
}
我有以下查询:
fromCategory('MyCategory')
.whenAny(function(s, e) {
linkTo("Foo", e);
})
上面的内容并没有做太多的事情,只是将所有的流聚合成一个。是否有可能转换出更平坦的 View ,例如这样的东西?也许我的电线交叉了,但显然使用 emit
可以实现这一点?
{
string Reference;
string CurrencyCode;
decimal PayingAmount;
}
我的想法是,一旦我写入流,我就可以保证聚合处于有效状态,因此对于任何感兴趣的各方,我应该只公开这些过程所需的字段。投影一个简单的模型(去标准化 View )是正确的做法吗?
上面是一个简单的示例,但您可以想象聚合会更复杂一些。
如果我遗漏了任何内容或需要进一步说明,请发表评论,我可以添加。
最佳答案
您正在寻找标准事件类别预测。
它向 steam 发出称为“$ce-”的链接事件。那里的类别是您的对象类型。
例如,您的聚合类型为 Order
,并且您将事件 OrderCreated
、OrderLineAdded
等写入名为 Order 的流-1
、Order-2
,其中 1 和 3 是您的聚合根 ID。然后,$ce-Order
流将包含该类型的所有聚合的所有事件。
看看 standard projections documentation .
通常,这正是创建读取端投影的方法 - 通过在类别流上创建追赶订阅并相应地更新读取模型。
为了运行投影,您需要使用 --run-projections=all --start-standard-projections=true
来查看其工作情况。
关于javascript - EventStore 预测何时使用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41440256/