Sequence of commands

I’m having conflicting results relying on the order of commands. For example, I have a Aggregate that does very little:

Another thing, if on a aggregate I have to rely in results from another aggregate, which option is the best from a reliability point of view?

@CommandHandler
public TaskAggregate(CreateTaskCommand command) throws Exception {
    Object something = commandGateway.sendAndWait(new RegisterSomething(command.getSomething());
    if(something == OK){
       TaskCreatedEvent event = (TaskCreatedEvent) TaskFactoryUtils.createEvent(TaskCreatedEvent.class, command);
       apply(event);
    }else{
       //do something else
}

or rely on Sagas that will be much more verbose?

@EventHandler
public void on(TaskCreatedEvent event) throws Exception {
    commandGateway.send(new RegisterSomething(command.getSomething());
}

And the bonus question… :slight_smile:

When something goes wrong in my aggregate I raise a notification:

TaskFailedNotification fault = (TaskFailedNotification) TaskFactoryUtils.createEvent(TaskFailedNotification.class, command);
eventTemplate.publishEvent(fault);

This is going directly to the event template and not thru the aggregate event sourcing. Are there any problems with this? Should I do ionstead

TaskFailedNotification fault = (TaskFailedNotification) TaskFactoryUtils.createEvent(TaskFailedNotification.class, command);
**apply**(fault);

Thanks again.

I would never design an Aggregate to send commands to another aggregate, let alone rely on its result. It’s what Saga’s are designed for.

It’s not a bad thing to publish events from an aggregate without applying them. However, do realize that some day you might discover that the information in the event would have been valuable enough to include in the event store.

Cheers,

Allard

Hi, thanks for the reply.

Yes, I always use Sagas for that kind of interaction, I try to keep the use of external sources in the Aggregate to access read model and mostly for validations. However people in my team tend to use the command from aggregate to another aggregate, and I have some difficult explaining why.

Regarding the events, you are right, I added a comment “REVIEW do we want this to be sourced? If yes, apply() instead” on that line to decide before going to production.

Finally, can you take a look at the first question about the reliability of the order of commands, specially if the order of commands is guaranteed, and if using different types of Command Bus may change that?

Thanks again for your help.

Cheers.

Inside the aggregate, the handlers are invoked directly, and in the thread that calls apply. Only when events are applied from within an @EventSourcingHandler, the event is handled in the aggregate after the handler completes.

This is completely independent of the CommandBus in use.

Cheers,

Allard