Command Handler outside the aggregate

Hello,

I am using Axon 4.X

My Structure looks like this

`

  • AggregatePackage
  • Aggregate1
  • CommandHandlerPackage
  • CommandHandler (Aggregate1)

`

When my command handler is in separate package and class

`

java.lang.IllegalStateException: Cannot request current Scope if none is active
at org.axonframework.messaging.Scope.getCurrentScope(Scope.java:53) ~[axon-messaging-4.0.3.jar:4.0.3]
at org.axonframework.modelling.command.AggregateLifecycle.getInstance(AggregateLifecycle.java:124) ~[axon-modelling-4.0.3.jar:4.0.3]
at org.axonframework.modelling.command.AggregateLifecycle.apply(AggregateLifecycle.java:62) ~[axon-modelling-4.0.3.jar:4.0.3]

`

Reading a lot of documentation and previous posts I see that it is recommended to have the @CommandHandler inside the aggregate class for commands that create the aggregate instance for e.g CreateProduct but we can have the commands outside the aggregate class for commands that are applied on an existing aggregate for e.g UpdateProduct

When I worked with Axon 3.X I remember to have all the commands outside the aggregate class and it worked perfectly fine.

Please advice.

Hi,

I‘ve found this:
https://docs.axoniq.io/reference-guide/implementing-domain-logic/command-handling/external-command-handler

It should be possible if called via CommandBus and implemented as described.

Most important: I can’t recommend organizing classes in technical packages. They should be grouped domain wise. Instead of „aggregates“ and „commandhandlers“ they should - at least to some extend - be named „product“…
It’s ok tough to separate command and query side or things dependency-wise.

Best regards
J

Thanks Johnny,

I realised that i would need to load the repo’s explicitely if I want to do that. Axon documentation was helpful