I have a question regarding returning values from a method on a custom command gateway. Briefly, it appears I cannot return the aggregate directly from its constructor. When I try this, the aggregate is constructed but the command gateway method always returns null.
In detail, say I have a simple custom command gateway :
public interface MyCommandGateway {
MyAggregate createMyAggregate(CreateMyAggregateCommand command);
}
In my aggregate, I attach the command handler to the constructor :
public class MyAggregate extends AbstractAnnotatedAggregateRoot {
@AggregateIdentifier
private MyAggregateId myAggregateId;
@CommandHandler
public MyAggregate(CreateMyAggregateCommand command) {
apply(new MyAggregateCreatedEvent(command.myAggregateId()));
}
@EventHandler
protected void handle(MyAggregateCreatedEvent event) {
myAggregateId = event.myAggregateId();
…
}
}
I am using the default command bus and event bus. I’m using Spring to configure the custom command gateway factory, enabling injection of the gateway where needed :
In the main program, I can quite happily cause the aggregate to be created, but the command gateway does not return it :
public class Main {
…
MyAggregate myAggregate = myCommandGateway.createMyAggregate(new CreateMyAggregateCommand(new AggregateId()));
// myAggregate is always null
}
Can anyone point out where I may be going wrong? I struggled to find any examples that use custom command gateways.
Thanks,
Steffan