Hi, so I’m learning about the axon framework and just want to solidify my understanding of the @TargetAggregateIdentifier
annotation.
My Command:
public class IssueCardCommand {
private String cardId;
private String versionNumber;
private Integer amount;
@TargetAggregateIdentifier
private String getAggregateIdentifier() {
return (null != versionNumber) ? cardId + "V" + versionNumber : cardId;
}
}
My Aggregate:
@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();
}
}
So this all works as expected and the events are being stored correctly. However, I just want to make sure that I understand the @TargetAggregateIdentifier & @AggregateIdentifier annotations correctly.
So, the
@TargetAggregateIdentifier - a command goes to a specific instance of the aggregate and so it is needed to tell the framework which instance it is, so this annotation on a field/method is used to load the events for that particular aggregate?
I noticed that when I didn’t have @TargetAggregateIdentifier in the command for the constructor, the code still works. But if it was missing from any subsequent commands it gave the error ‘Invalid command, It does not identify the target aggregate’, which I feel like confirmed my understanding above?
@AggregateIdentifier - this tells the axon framework that this is the identifier, so that when more commands are introduced it will need to store the events for that particular aggregate?
I would really appreciate it if someone could point out if my understanding is correct and comment where it isn’t, so that I can use the framework in the correct way.
Thanks.