I’m having some questions regards state propagation and making the cost testable. The application is structured in a way that the concurrency comes from the HTTP requests, so the webserver will create a new process for each request. Once the request hits my code the code doesn’t have any parallel execution or things like GenServers at all. It’s plain functions calling functions.
And now is the question, imagine that I have 3 layers:
- handler/http
- service
- persistence
The handler calls the service and the service calls the persistence. But to avoid hardcoding the dependencies I need to have some way to pass these values around. My first try was with a centralized state agent, it works but not ideal, I can’t even run parallel tests because they all depend on the same global state agent. My next approach would be to do something like a context that is propagated through all the requests. What do you guys think of this approach?
I should always depend on the services that are passed to my function, so my signature would be something like this:
def send_link(%{user_service: user_service, expiration: expiration}, email) do
end
Instead of this code have a hard dependency on some service, like the user service, it can simply receive it as the first argument.
Wait for some opinions, is this good? should I do something different? Any recommendation of docs to learn more about state management with Elixir?