0

I have a project developed using Domain driven design principles. It is CQRS based using axon framework. I have a scenario where I need where on a particular command I need to generate a document in the aggregate using aggregate's state. I do not need to store the id of the generated document in the aggregate. But I need to publish an event from the aggregate with the id of the generated document because another domain needs that id.

Is it a good practice to fire a command not for updating aggregate state, but for doing some processing and publishing event for updating other aggregate?

Also is it a good practice to publish an event from aggregate not for sourcing, only for updating another domain?

Ishrat Jahan
  • 136
  • 5

2 Answers2

1

So basically you want to use an AR as a factory for another AR (Document in this case)? This is quite common actually and helps being faithful to the ubiquitous language rather than spawning ARs from nowhere.

I'm not sure how AXON command handlers work and how the state is persisted afterwards, but here's how I'd do it:

//Handler
Document doc = someAggregate.generateDocument(id, ...);
documentRepository.save(doc);

If you really don't need the Document AR you could just create the event directly:

DocumentGenerated event = someAggregate.generateDocument(id, ...);
eventStore.append(event); 

The DocumentGenerated event can then be dispatched to the other context using any messaging infrastructure you have in place.

plalx
  • 42,889
  • 6
  • 74
  • 90
  • 1
    Axon Framework allows the creation of Aggregates from another Aggregate to cope with this. You can do that by using the `AggregateLifecycle#createNew(Class, Callable)` function from within the scope of an aggregate. – Steven Dec 19 '18 at 09:50
1

I think that maybe the operation of generating the document should be a domain service. And the domain event would be generated by the domain service, which is not usual , but it is possible.

Of course you can publish domain events not for sourcing. In fact, you can do CQRS without ES. Events are a way of async communication between BCs.

choquero70
  • 4,470
  • 2
  • 28
  • 48