0

After an aggregate has been deleted using the method markDeleted() , what is the correct way of handling a request using the same aggregate identifier?

The markDeleted() method marks and aggregate as deleted, when a create event is then attempted using the same aggregate identifier, an exception is thrown.

Is it just a case of a try/catch?

If I need to be more clear, let me know.

Thanks in advance,

P.S) Hi Allard! :)

howells699
  • 148
  • 14
  • 1
    What exactly are you asking? I did not worked with Axon, but common sense is you should not accept any commands for aggregates in Deleted state - maybe except "Undelete". – Roman Eremin Aug 16 '18 at 14:26
  • There is potential that a create request with the same aggregate identifier could occur. Therefore this needs error handling. What I want to know is how to do so in the correct way. Thanks – howells699 Aug 16 '18 at 14:48
  • 1
    It has not been mentioned here yet so I'd like to add that you can always implement custom delete functionality without relying on axon. For instance, the aggregate itself could know whether it is currently deleted. Then you can do stuff like reviving/resetting aggregates using an existing id. I can think of quite some use cases... – Doe Johnson Oct 20 '18 at 14:52

1 Answers1

2

I'd suggest against reusing the Aggregate Identifier of a deleted Aggregate instance.

Axon can go two ways when you're doing stuff like this: 1. The old Aggregate Identifier is reused in a Command which will instantiate an Aggregate (I like to call them 'Constructor Command Handlers'). In that case an exception is thrown in the Event Storage Engine stating the given event already exists. 2. The old Aggregate Identifier is reused for a Command which performs some decision making. In other words, this Command will have the @TargetAggregateIdentifier annotation tied to the old Aggregate Identifier. In this scenario the Aggregate will be Event Sourced (assuming you're doing Event Sourcing), up to the Event which marks the Aggregate to 'deleted'. Then you'll get another exception in your face stating that this is incorrect behavior.

If you really need to reuse that Aggregate Identifier, you will have to remove the events for that old aggregate you have deleted; very likely not the thing you want to do.

As such it is better to use a UUID, which (almost) guarantees you of a unique aggregate identifier. That way you can safely mark aggregate as deleted and not be bothered with handling exceptions in this space.

P.S. I'll say hi to Allard for you.

Steven
  • 6,936
  • 1
  • 16
  • 31