如何从 AxonFramework 或任何其他事件溯源框架中的数据库加载聚合对象?

ahe*_*ero 1 frameworks domain-driven-design event-sourcing axon

我有这个问题很久了。对于 Internet 上的大多数示例。他们总是先创建一个聚合对象,然后再操作聚合对象。我的问题是,除了每次都创建一个之外,如何从数据库加载一个。我将以 e-sopping 为例。我将一种产品视为一个聚合对象。我无法将它们全部加载到我的程序内存中。那我该怎么办呢?

我所做的是,编写另一个带有参数 UpdateProductCommand 的构造函数以及带有参数 CreateProductCommand 的构造函数。在此构造函数中,我从数据库加载它。这样可以吗?

class Product{

    public Product(){}

    @CommandHandler
    public Product(CreateProductCommand command){
        apply(new CreateProductEvent(command.id));
    }

    @CommandHandler
    public Product(UpdateProductCommand command){
        load(command.id)
        ...
        apply(new UpdateProductEvent(command.id));
    }
}
Run Code Online (Sandbox Code Playgroud)

Luc*_*pos 5

我假设您想使用状态存储聚合,您可以查看链接以获取更多信息。

为了给您一些启发,我必须看看您标记了哪个字段@Id@AggregateIdentifier但假设您有一个String id(也是您的command.id@TargetAggregateIdentifier,Axon 负责根据该字段从数据库加载聚合。话虽如此,您不必自己处理它,只需专注于业务逻辑(这意味着验证)并在需要时应用新值即可。