Strategy pattern

Hi all.

I’m giving Axon a try with a small test project but I’ve hit a roadblock.

My domain object “Game” has different behavior depending on what level number is being played. This would usually be handled using the Strategy pattern and passing the strategy via the constructor to the domain object. It seems that would be incorrect when using Axon because I might only know the level number after object initialization (when replaying events). So I think I should pass in a StrategyFactory and let the @EventSourcingHandler annotated method instantiate a strategy. But I still have no clue as to how to pass in this factory object. Can’t be via constructor because Axon calls the constructor, not me.

Putting the factory object inside the event also makes no sense, as it would be persisted and propagated to all listeners. All the listeners should get is the level number (an integer).

Any ideas?

Hi Joao,

if the strategy depends on the level, you could choose your strategy the component that wraps the level (Game?).
You can still pass the strategy in the constructor. Simply make sure the strategy is serializable and passed along in the events. In that case, the strategy will be available when reconstructing the aggregate state.

Alternatively, you could consider using named strategies. In that case, you only need to pass the name, and use a ‘Repository’ to get an instance for a specific strategy name.

If you use Spring, you can inject beans by adding them as a parameter to the @CommandHandler method. It doesn’t work for @EventHandler methods.

Last option is to implement a ParameterResolverFactory instance (check javadoc on how this works). That allows you to define custom parameter types for your @EventHandlers, @CommandHandlers, etc…

Hope this helps.