gpt4 book ai didi

cqrs - @AggregateIdentifier & @TargetAggregateIdentifier 的理解

转载 作者:行者123 更新时间:2023-12-01 23:30:31 27 4
gpt4 key购买 nike

所以我正在学习轴突框架,只是想巩固我对 @TargetAggregateIdentifier 注释的理解。

我的命令:

public class IssueCardCommand {

private String cardId;
private String versionNumber;
private Integer amount;

@TargetAggregateIdentifier
private String getAggregateIdentifier() {
return (null != versionNumber) ? cardId + "V" + versionNumber : cardId;
}
}

我的聚合:

@Aggregate
@Slf4j
public class GiftCard {

private String giftCardId;
private String versionNumber;
private Integer amount;

@AggregateIdentifier
private String getAggregateIdentifier() {
return (null != versionNumber) ? giftCardId + "V" + versionNumber : giftCardId;
}

public GiftCard() {
log.info("empty noargs constructor");
}

@CommandHandler
public GiftCard(IssueCardCommand cmd) {
log.info("handling {}",cmd);
//this.giftCardId = cmd.getCardId();
//this.versionNumber = cmd.getVersionNumber();
apply(new CardIssuedEvent(cmd.getCardId(),cmd.getVersionNumber(),cmd.getAmount()));
}

@EventSourcingHandler
public void onCardIssuedEvent(CardIssuedEvent evt) {
log.info("applying {}",evt);
this.giftCardId = evt.getCardId();
this.versionNumber = evt.getVersionNumber();
this.amount = evt.getAmount();
}
}

所以这一切都按预期工作并且事件被正确存储。但是,我只想确保我正确理解了@TargetAggregateIdentifier 和@AggregateIdentifier 注释。

所以,

@TargetAggregateIdentifier - 命令转到聚合的特定实例,因此需要告诉框架它是哪个实例,因此字段/方法上的此注释用于加载该特定聚合的事件?

我注意到,当我在构造函数的命令中没有 @TargetAggregateIdentifier 时,代码仍然有效。但是,如果它在任何后续命令中丢失,它会给出错误“无效命令,它无法识别目标聚合”,我觉得这证实了我上面的理解?

@AggregateIdentifier - 这告诉轴突框架这是标识符,以便在引入更多命令时它需要存储该特定聚合的事件?

如果有人能指出我的理解是否正确并在不正确的地方发表评论,我将不胜感激,这样我才能以正确的方式使用该框架。

谢谢。

最佳答案

我已经提供了 answer在最初发布问题的 AxonIQ 讨论平台上。

因为用链接回答只会违反 SO 的礼节,所以我也在这里应对我的回答:

Your understanding is correct I guess. But let me give you asimplified explanation of the process so you can compare it to yourcurrent understanding.

When you issue a command that creates new aggregate (one that ishandled by the Aggregate's constructor) there is no need foridentifier. The reason is you are creating a new instance, not loadingan existing one. In that case the framework only needs the FQCN of theaggregate in order to create an instance of it. It can easily findthe FQCN because it knows (from inspection during aggregateregistration) which aggregate can handle such create command. That'swhy create commands without @TargetAggregateIdentifier work justfine.

Once the create command is processed the aggregate's state needs to bestored somewhere.

  • for event sourced aggregates all state changing events are stored in a event store
  • for state stored aggregates the entire state is stored in a repository

In both cases the framework needs to know how to identify this data.That's what @AggregateIdentifier is for. It tells the framework tostore the data in way that it is identifiable by specific identifier.You can think of it as primary key in DB terms.

When you send a command to an existing instance of an aggregate youneed to tell which instance that is. You do so by providing a@TargetAggregateIdentifier . The framework will then create a newempty instance of the respective aggregate and then try to load thedata into it

  • for event sourced aggregates by reading all the past events related to that instance
  • for state stored aggregates by reading current state from a repository

In both cases the framework will search for data that's identifiableby the value of @TargetAggregateIdentifier. Once the aggregate datais loaded it will proceed with handling the command.

关于cqrs - @AggregateIdentifier & @TargetAggregateIdentifier 的理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66355005/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com