pmjoe

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?

Showing Posts 1 to 1

al2o3cr

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:

user_service.some_function(arg1, arg2)

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):

# real code should do a lot moar error handling
{:ok, result, commands} = ServiceModule.do_stuff(args)
:ok = Persistence.execute(commands)

Here commands might 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_stuff can be inspected, pattern-matched, and asserted on in tests without any implementation of Persistence at all.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews