How to create aggregate roots when there is no dedicated create command?

Hi,

I am new to Axon and struggling a bit to write a prototype for a very simple service which can be summarized as follows:

  • It stores immutable documents
  • Documents are identified by an id and a version number (both are client provided)
  • Version number are not expected to be continuous (client are free to use the versioning scheme they like)
  • Clients can push versions out of order
  • Trying to store a document for an already existing document id & version number should be rejected, or accepted if and only the payload is exactly the same, or always accepted (business rules aren’t defined yet).

Command & domain layers are very thin, meat of the service will in queries and I expect ES / CQRS will be useful for that.

My problem is that I don’t know how to manage aggregate roots, more especially how and when to create them. Unlike Axon-bank & Axon-trader, it seems that I don’t have a dedicated command to create an aggregate root. The only command I have is “StoreDocument”. I expect the command handler to automatically creates the aggregate root if needed.

I have tried several different approaches but none of them satisfies me or even work. Any advice or code sample would be appreciated. Or is my modeling just plain wrong?

Thanks!

Clément

You could have the command processed by a service (outside of Axon) that first checks if the document exists. If not it sends CreateDocument, followed by the StoreDocument. I would call the latter event then rather “StoreDocumentVersion”.

Jorg

Hi Clément,

I’d probably too go with what Jorg is suggesting here.
Have a service after the controller and in front of the command bus which checks if a creation is in place.

What’s the aggregate root you’ve got in mind now actually?
Maybe that might change the answer I’d give :slight_smile:

Hope this helps.

Cheers,

Steven