Hi,
i am curious on best practices to implement relationships between aggregates. E.g. we got two aggregates Persons and Groups. I think i am right when i say this is no case for Multi-Entity-Aggregates as a Person can exist without a group and probably might be in multiple groups. If i want to add a Person to a Group i have to make sure that the Person as well as the Group exist. So i could think of three ways of dooing that:
a) add an validation layer that verifies that the corresponding Person and Group exist before the command to add the Person to the Group is issued.
Pros: no validation in commandhandler so it can easily be placed on the aggregate. This also enables us to use unit testing capabilities of axon.
Cons: no enforcement of the conditions, developers can just send non validated commands of that type.
In the unlikely event that the group was deleted but the query data wasn’t updated at that point in time we will have a corrupted state.
b) add an external command handler hat verifies that the corresponding Person and Group exist
Pros: Commands will always be validated.
Cons. I believe we can’t easily unit test this with the AggregateFixture?
In the unlikely event that the group was deleted but the query data wasn’t updated at that point in time we will have a corrupted state.
c) add two commands one to create the association and one to request it beforehand. The request will go to the Group which will only be able to handle the command if it does exist. If it accepts it will emit an event. That event is received by a Saga - that will handle the association over time - and sends the command to add the user to the group to the user. If the user doesnt exist we could eventually end the saga after some deadline. Otherwise the user will emit an success event. If the Group is deleted the saga will take care of producing a consitent state.
Pros: Conditions are enforced and not vunerable to stale state of read model.
Can be Unit tested with axon capailities
Cons: developers can still accidentally choose to use the Add Command instead of the request command
In the case i didn’t miss something important i would go with c but is there a way to mitigate missuse of the add command?
Kind regards
Nils