commanded / commanded

Use Commanded to build Elixir CQRS/ES applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question about process managers and remote API calls

adampointer opened this issue · comments

Hello,

First off, thanks for all the great work being done in this repo!

I have a process manager which responds to a user initiated event from aggregate A and in response will fire off multiple commands to aggregate B before finally sending another command to aggregate A. Within this flow, I need some data from some remote APIs. This data will directly effect the content of the commands dispatched from the process manager.

My question is where best to call the logic which calls out to the remote APIs? I can either put it in aggregate A and record the results in the domain event which initiates the process manager, or simply keep it within the process manager. I am leaning towards doing it in the aggregate as then the API call result is persisted in an event. However this feels like I am bloating the aggregate code. Is there another alternative or best practice that I am missing?

Thoughts?

(Not a maintainer, just a happy Commanded user here).

Some patterns I've used with an API-heavy project using commanded:

Attaching external data to a command can be done with the Command Enrichment middleware: https://gist.github.com/slashdotdash/811d754951b2573a82ff14fe8506012e. That works well for remote read operations.

For external writes, we route the command to an explicit Handler module, where it can make the API calls and pass the request/response along with the command to a function in the aggregate module.

We don't do any remote API calls in process managers, or dispatch commands who's handlers make API calls from process managers. Instead we enqueue Oban jobs from event handlers to ensure that the process manager isn't a bottleneck (due to the shared event store subscription for all instances of a Process Manager), and allows for the command to be retried if dispatch fails due to the external API being unavailable. The downside is we don't get the the process manager state handling or causation_id/correlation_id values being automatically propagated between events/commands.

Thanks @mbuhot the command enrichment middleware looks to be exactly what I was looking for. It will nicely separate out the remote API logic from my aggregate and PM modules.