Aggregate Root/CommandHandler => where to put my referring services?

Hi!

I already saw this thread regarding the above topic:
https://groups.google.com/forum/#!searchin/axonframework/ar$20service|sort:date/axonframework/nENWpUoqm-c/NIOMH_UkBQAJ

It has been suggested that all Services needed for the aggregate state transformation should be moved to the command handler.

Guess that a referring service only needs to be called in 1% of all cases.
Should the commandhandler gather everytime all the potentially needed data for the aggregate?

Is there a way to inject spring beans into the aggregate root and let the AR decide what data is needed for a specific business function?

Hi,

you basically have a number of options. Adding contextual information to commands is one of them. Alternatives are to fetch this information from within the command handler outside the aggregate, or even from within the aggregate. All options have pros and cons.

In the latter two cases, you can use Axon’s Parameter Resolution mechanism to add the service that you need as a parameter to your @CommandHandler (or even @EventHandler) methods. If you use Spring, any Spring bean of which there is exactly one instance can be injected like this. If you don’t use Spring, you can use Axon’s configuration API to build your infrastructure. In that case, all components registered with the Configurer can be used as parameters to your handler methods.

The only thing to keep in mind is, that when this call is done from withing the aggregate, the aggregate is holding a lock preventing others to invoke it. So you’d want to make sure these service calls are done within a time that matches your performance requirements for the command model.

Hope this helps.
Cheers,

Allard

Hi Allard,

so am I getting it right that I am not able to use Spring Beans in the business methods of the aggregate?
Only in @CommandHandler and @EventHandler Methods?

cu,
Dirk

Hi Herr,

The only way to wire beans in your aggregate is by message handling functions like @CommandHandler and @EventHandler annotated functions, since the ParameterResolvers are only used for handler annotated functions.
So unless those business methods are called from your message handling functions, then the answer to your question is yes.

Hope this helps!

Cheers,

Steven

Thank Steven,

I think what a lot of people do is something like this:

<util:list id="eskalationsMemoIds" list-class="java.util.ArrayList">
   <value>myFirstConstantValue</value>
   <value>mySecondConstantValue</value>
</util:list>

Like defining constants using spring. I would like to use these data in my Aggregate Business functions.
Since these values are implementation details, I don’t like to pass these from my commandhandler to the business functions of my aggregate.

The aggregate should simply use it in its business functions. So typically you would use Spring to inject it into the aggregate.

This would be very useful, wouldn’t it? Is something planned for axon that way? Is there another workaround to achieve this?

Hi,

most attempt to inject data I have seen so far were using typed beans. So an @Component annotated class (I was told XML is sooo 2016) that contains a number of properties. Some of these properties will be hard-coded, some others from property files, etc. Actually, Spring Boot provides a very nice mechanism for this, using @ConfigurationProperties. It allows a bean containing properties to be configured using settings in the application.yml/properties file.

However, it does sounds like a good idea to allow for @Qualifier annotations on parameters to indicate which instance of a Spring bean should be injected, if multiple exist. That should also allow you to resolve a list of properties.

Will have a look into it.

Cheers,

Allard