I’m faced with an odd problem where I have to implicitly create an aggregate root and am looking for a nice solution.
The problem I face is that I receive events from an external system which may (and in fact does) send events in an undefined order. I can calculate a type-5 UUID for the aggregate root, however when handling the external event I do not know if the aggregate root already exists or not. Ideally I would use a factory in the AxonFramework to create the aggregate root using a command-handling constructor that only accepts the UUID and whenever a command to handle the external event is processed.
What would you suggest that I should try. I’ve been thinking about the following options:
- using read model to keep track of which aggregate roots have already created and before sending the command for the external event create the aggregate root using a creational command
- using an intercepter to modify the command (if that’s even possible, I don’t know if I can access the repository in an interceptor, I haven’t tried that)
- use an “external” command handler (i.e. one that is not part of the aggregate root) and try to load the aggregate root when handling the command, if it does not exist then manually add a fresh aggregate root, add the creation event and then add the event for the external command.
For me solution 3) looks most appealing since 1) relies on the caller knowing about internal state of the system. About number 2) I’m not sure if that’s even possible when switching to a command bus implementation that is distributed. As for number 3), that’s the way I wanted to go, however I’m not sure how I’d really have to implement the command handler using only the UnitOfWork as an interface to the AxonFramework. Any tips here?
Thanks for any ideas or input!