We are looking at Axon and saw the following pattern applied in more than one sample application. This snippet below is from the AddressBook sample in the axon github repo.
// In ContactCommandHandler.java
Contact contact = new Contact(contactId, command.getNewContactName());
repository.add(contact);
…
// In Contact.java
public Contact(String identifier, String name) {
apply(new ContactCreatedEvent(identifier, name));
}
…
// In Contact.java
@EventHandler
protected void handleContactCreatedEvent(ContactCreatedEvent event) {
this.id = event.getContactId();
}
The id of the aggregate root is set when handling the ContactCreatedEvent as opposed to being done in the constructor. This seems a bit counter-intuitive and also dangerous. Surely we are missing something here. Can someone who is more knowledgeable with the framework kindly clarify?
Thanks!