5

给定带有字段的事件存储:

  • AggregateId : 整数
  • 有效载荷:blob
  • 版本:整数

其中包含基于以下事件的事件:

public class OrderLineAdded
{
    int Id;
    short Quantity;
}

...然后添加了更新结构的更多事件:

public class OrderLineAdded
{
    int ProductId; // field name has changed
    int Quantity; // field datatype has changed
}

当检索到这些历史数据(用于分析等)时,如何将二进制有效负载重构为有意义的数据?

注意:上面的代码不是一个好的事件/事件存储实现的例子。我只想知道应该如何处理这种情况。

4

2 回答 2

3

Rinat Abdullin 在他的 CQRS bliki 中概述了一些域事件版本控制策略,您可能会觉得这些策略对阅读有用。

https://abdullin.com/post/event-sourcing-versioning/

他提出了两种可能的方法:

  1. 使用可以自动处理事件中属性重命名的序列化程序,例如 Google 的 ProtoBuf 序列化程序。
  2. 实现以下接口以手动升级内存中的事件。
public interface IUpgradeDomainEvents
{
    IEnumerable<IDomainEvent> Upgrade(IDomainEvent e, string id);
}
于 2012-02-07T19:04:08.000 回答
1

我见过的处理向后兼容性的唯一方法是对事件的每次更改进行版本控制。有些人在不同的命名空间中创建新版本,以便命名空间反映版本/功能信息:

namespace Foo.ProjectA.Release1
{
  public interface ISomeEvent {...}
}

namespace Foo.ProjectA.Release3
{
  public interface ISomeEvent {...}
}

或者,使用类名中的版本/功能信息创建类的新版本:

namespace Foo.ProjectA
{
  public interface ISomeEvent {...}
  public interface ISomeEvent_MoreRefinedVersion {...}
}

就个人而言,我更喜欢前一种方法,除非新版本为事件添加了更具体的语义含义。

于 2012-02-07T02:29:43.250 回答