Returning multiple events from command

From the existing post, One command multiple domain events ,
does the current framework support to send multiple events from apply.
And if support is, should we be passing the list object to apply ?

Hi @mnkartik,

The post you shared I believe is about Axon 1.
We are at 4.5.3 as we speak.

Sure, the support is there but not as a list.
If you check the code for the AggregateLifecycle you will notice the apply method returns an ApplyMore instance. Checking the code for this one and you will see it has 2 useful methods: andThenApply and andThen which can be used to “chain” calls.

That’s how I would use send multiple events from the Aggregate.
Of course, there is nothing wrong with calling apply several times as well.

KR,

Sorry for the delayed response.
When multiple events are sent, are they published as a single unit or none if any failure ?

Hi @mnkartik,

Yes, if you send multiple events from a given Aggregate (using the methods mentioned before), they are all part of the single ‘transaction’ which is managed by the UnitOfWork.

You can have different behaviours depending on the type of processor you are using (streaming vs subscribing) but that’s another history. Let me know if you want to know more about it as well.

KR,

On this note, from above,

Of course, there is nothing wrong with calling apply several times as well.

Does this mean that only when we chain, the events are part of unit of work or even when we call apply() independently from command will always result in unit of work ?

Even if you call independently they will be part of the same Unit Of Work as the whole ‘transaction’.

KR,

Than you @lfgcampos.

Just for the understanding, so if command handler triggers multiple events Event1 and Event2 and in one of the event handler ( say for Event2 ), if some exception occurs then none of the events are recorded into registry and the state of the aggregate would be the same as of the one before triggering the command handler.

This part depends on which type of Processor you are using as I shared before:

You can have different behaviours depending on the type of processor you are using (streaming vs subscribing) but that’s another history. Let me know if you want to know more about it as well.

Very simply put, this is true for SubscribingEventProcessor and false for StreamingEventProcessor. I strongly recommend you to have a look at our docs about this here.

KR,

Thank you @lfgcampos . Will go through the docs for further understanding.