Is there a spring config for CommandHandlers on Aggregates?

Hi, I’ve moved my command handlers to some of my aggregates but they don’t seem to be properly registered even though I have axon:annotation-config etc setup in my spring config. The only thing on this in the docs the following code. Do I need to set this up at start up?

AggregateAnnotationCommandHandler handler = AggregateAnnotationCommandHandler.subscribe(MyAggregate.class, repository, commandBus);

Hi Erich,

you need to configure a command handler in spring, like this:

<axon:aggregate-command-handler aggregate-type=“com.my.company.Aggregate”/>
(Note that the exact name might be a bit different. I son’t have access to the code here)

Cheers,

Allard

Thanks Allard. I have another quick question. When using this approach, what the best way to handle validation? If for instnace I have a User {String id,String email,…} aggregate and a CreateUser(id,email) command, and I want to ensure that I’m not creating a user with a duplicate email, with the separate command handler class, I’d just check the read model or something in the handler method. But that doesn’t seem like a good approach from within the aggregate instance, or perhaps it is, just not sure.

also in looking at some of U. Dahan’s stuff on ‘well behaved clients’, I’m wondering if my say ‘UserService’ that’s actually building and sending the CreateUser message should be doing the check before dropping it into the CommandGateway.

Personally, I prefer to do that outside of the aggregate. So I’d have a CommandHandler on a bean, that loads the aggregate from a repository (or in your case adds it) when validation passes. You can choose to use the query model or if you need more consistency, a separate model that is updated by the command handler when a username is chosen.

Cheers,

Allard