Custom command gateway not returning aggregate from constructor

485 views
Skip to first unread message

steffan....@clicktravel.com

unread,
Jul 26, 2013, 8:15:36 AM7/26/13
to axonfr...@googlegroups.com
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<MyAggregateId> {
@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 :

<bean id="myCommandGatewayFactory" class="org.axonframework.commandhandling.gateway.CommandGatewayFactoryBean">
<property name="commandBus" ref="commandBus"/>
<property name="gatewayInterface" value="org.steffan.MyCommandGateway"/>
</bean>

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


Click Travel Ltd
Taking business travel and expenses one step further

Winner of "Best Business Travel Management Company" at the Business Travel Awards (2012 & 2009) and the Travel Trade Gazette Awards (2011). We're also ranked 22nd in the 2012 Sunday Times Hiscox Tech Track 100.

For all the latest Click news please visit our blog: http://clicktravel.com/blog

Think: Do you really need to print this email?

Allard Buijze

unread,
Jul 26, 2013, 9:56:10 AM7/26/13
to Axon Framework Users
Hi Steffan,

the reason you're getting a null response is because the @CommandHandler on a constructor always has a null return value for the command. Currently, the only way to get another return value is by using a "normal" command handler class.

In CQRS, it is considered bad practice to expose your Aggregate to the senders of commands. The only way to interact with an aggregate is by sending it commands. If you would return it, you suddenly have other options. Even when you use your command model for querying, I'd suggest not to do this. Instead, create a (Query) Repository that returns the (same) aggregate.

Kind regards,

Allard Buijze


--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages