pmjoe
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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 1- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
It’s arguable whether dependency injection in this style makes code “more testable”, but it’s not debatable that it makes it much harder for the compiler and other tools. Code like:
can’t be checked at compile time for correctness like
ActualUserService.some_function(arg1, arg2)can.I’ve seen this cause failures more than once in applications with strongly-injected “unit tests” that featured mocks that didn’t have the correct signatures. 100% code coverage, all green, 100% broken in production.
Instead of injecting a module for “persistence” into the functions in the “service”, consider approaches like defunctionalization that would instead make the “service” and “persistence” functions that are chained together by the integrating code (in the handler):
Here
commandsmight be a list of tuples like{:update, some_id, new_value}or more structured things like{:insert, %Ecto.Changeset{}}.This allows each module to be tested cleanly in isolation - the output of
ServiceModule.do_stuffcan be inspected, pattern-matched, and asserted on in tests without any implementation ofPersistenceat all.