2
votes

I honestly have no idea where to begin. The repository aspect is relatively simple but I cannot seem to find any information on how to delete an aggregate root via the CommandGateway.

Any directions and/or documentation on how to achieve this would be greatly appreciated.

1
Do you use an Event Sourced Aggregate, or a State Stored one? Note that the CommandGateway has nothing to do with this in either case, as it is just the API to dispatch Command Messages. - Allard
I am using the Event Sourced Aggregate. The digging I have done leads me to believe I need to implement a repository that allows me to directly delete the DomainEventEntry using the aggregate id. Is it advisable to do this? - Kenneth Clark
Apologies for another comment. Further research indicates the presence of markDeleted() - I assume this retains the history of the events that led up to the aggregate being "deleted" and is the preferred mechanism when using Event Sourcing? - Kenneth Clark
That's correct. With Event Sourcing, "delete" doesn't really exist. It's just a state like any other, except that on a "deleted" state, all commands are rejected. - Allard
Thanks for your time Allard, really appreciate it. - Kenneth Clark

1 Answers

4
votes

Putting this here for future reference for anyone else that might be as lost as I was initially.

When using the Event Sourcing Aggregate, one can make use of the markDeleted() static method on the Aggregate in question. I placed mine in the @EventSourcingHandler

import static org.axonframework.modelling.command.AggregateLifecycle.markDeleted;

@EventSourcingHandler
public void on(DeletedEvent event){
    markDeleted();
}

Further information can be found at: https://docs.axoniq.io/reference-guide/implementing-domain-logic/command-handling/aggregate#aggregate-lifecycle-operations

To delete the view data associated with the aggregate I used an external @EventHandler:

@EventHandler
public void on(DeletedEvent event, ReplayStatus status){
    entityRepo.deleteById(event.getId());
}

Thanks to Allard for engaging me in the comments section.