Referring Services in Aggregate Root

Visto 98 veces
Saltar al primer mensaje no leído

Setya

no leída,
28 jun 2016, 11:55:2528/6/16
a Axon Framework Users
Hi all,

In my AR in order to run business process or validation I need information from some services, how do I inject these services into AR ? Is it even a good practice to ref services in AR?

Any advice would be greatly appreciated.

Thanks & Regards,

Jim

René de Waele

no leída,
28 jun 2016, 23:14:4228/6/16
a axonfr...@googlegroups.com
Hi Jim,

The easiest way is to add the service as a parameter of your command handler method:

@CommandHandler
void handle(SomeCommand command, YourService service) { ... }

In case you're using Spring the service is injected automatically. If not you need to register the service with the parameter resolver passed to the aggregate repository.

However, to answer your second question, it's often not a great idea to call services (or perform other potentially slow operations) from within your AR. The reason is that your aggregate instance is locked while it processes a command. Any command that takes a long time to process therefore severely limits the throughput of commands for that instance. Also the throughput is then a function of 'random' external factors like the latency of the service in question.

Often it's better to call the service before sending the command and add the result of the service invocation to the command. Note however that other commands may reach the AR instance while the service is being invoked, though in most cases that shouldn't matter.

Regards,
Rene
> --
> 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/d/optout.

Setts

no leída,
29 jun 2016, 10:14:3929/6/16
a axonfr...@googlegroups.com
Hi Rene,

Thank you for your advice.

I couldn't agree more that ARs should retain their purity as much as possible by not introducing service dependencies and "enriching" commands comes to my mind of how, but sometimes I feel it's not practical to do so.

Let's say I have Invoice AR, upon creation I need to validate that inventory is available. So before submitting command I have to 1st query inventory last balance and put it in the command for the AR to validate against. I'm afraid the more data needed by AR the more bussiness process I have to move out from AR making it anemic.

Regards,
Setya

John Heinnickel

no leída,
5 jul 2016, 15:24:125/7/16
a Axon Framework Users
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos