I have an Order
aggregate with the following commands:
CreateOrderCommand
PlaceOrderCommand
- ... (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
@Service
into the aggregate's@Autowired
constructor. - When the
PlaceOrderCommand
is issued, use the service to place the order onto the relevant execution venue and once done emit an event (eitherOrderPlacedSuccessfullyEvent
orErrorInOrderPlacementEvent
). - 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
@Service
into 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
@Autowired
constructor?
The other thing I was potentially considering was:
- having a
PlaceOrderInstructionCommand
(instead of the simplePlaceOrderCommand
) which emits aReceivedPlaceOrderInstructionEvent
that a separate event listener is listening for. - that event listener would have the relevant
@Service
injected into it and would do the placement of theOrder
. - then after placing the
Order
it 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?