I have an Order aggregate with the following commands:
CreateOrderCommandPlaceOrderCommand- ... (rest redacted as they are not pertinent to the question) ...
The PlaceOrderCommand refers to placing the Order onto an external execution venue. I have captured the behaviour for placing an order onto an external execution venue within a separate (non CQRS) @Service. However, I am struggling (due to lack of experience with Axon) with how best to connect my @Service with the aggregate.
My normal way of thinking would have me:
- Inject the
@Serviceinto the aggregate's@Autowiredconstructor. - When the
PlaceOrderCommandis issued, use the service to place the order onto the relevant execution venue and once done emit an event (eitherOrderPlacedSuccessfullyEventorErrorInOrderPlacementEvent). - Change the aggregate's state within the relevant
@EventSourcingHandler.
My question is:
- does my description above of how to handle this particular use case with Axon make sense (in particular injecting a
@Serviceinto an aggregate feels a bit off to me)? - Or is there a different best practice of how to model my scenario when using CQRS/event sourcing with Axon?
- Axon requires an empty constructor in the aggregate. How does this reconcile with having the
@Autowiredconstructor?
The other thing I was potentially considering was:
- having a
PlaceOrderInstructionCommand(instead of the simplePlaceOrderCommand) which emits aReceivedPlaceOrderInstructionEventthat a separate event listener is listening for. - that event listener would have the relevant
@Serviceinjected into it and would do the placement of theOrder. - then after placing the
Orderit would send a command (or should it emit an event?) to the aggregate informing it to update its state.
Could you please advise on what is best practice for modelling this scenario?