Hello everyone,
I’m new to Axon. I watched the getting started videos and I’m going through the AxonBank project.
In the videos and firsts commits of the project, the CreateBankAccountCommand handler looks like:
@AggregateRoot
public class BankAccount {
@AggregateIdentifier
private String id;
private long overdraftLimit;
private long balanceInCents;
@SuppressWarnings("unused")
private BankAccount() {
}
@CommandHandler
public BankAccount(CreateBankAccountCommand command) {
apply(new BankAccountCreatedEvent(command.getBankAccountId(), command.getOverdraftLimit()));
}
Later on, the same command is handled slightly differently, using a repository:
public class BankAccountCommandHandler {
private Repository<BankAccount> repository;
private EventBus eventBus;
public BankAccountCommandHandler(Repository<BankAccount> repository, EventBus eventBus) {
this.repository = repository;
this.eventBus = eventBus;
}
@CommandHandler
public void handle(CreateBankAccountCommand command) throws Exception {
repository.newInstance(() -> new BankAccount(command.getBankAccountId(), command.getOverdraftLimit()));
}
@Aggregate
public class BankAccount {
@AggregateIdentifier
private String id;
private long overdraftLimit;
private long balanceInCents;
@SuppressWarnings("unused")
private BankAccount() {
}
public BankAccount(String bankAccountId, long overdraftLimit) {
apply(new BankAccountCreatedEvent(bankAccountId, overdraftLimit));
}
My questions are:
- What’s the difference between the 2 approaches?
- Is the second one the new recommended way?
- Is this documented somewhere? (I couldn’t find it on the reference guide)
Thanks.
Luiz Signorelli