sulo

sulo

How to handle inter-module dependencies with Elixir?

Hi Everyone,

I’m currently learning Elixir and come mainly from a Java/Typescript background. I’m currently primarily using phoenix, but I think my question is more general and not exclusively related to phoenix.

How does one handle inter-module dependencies in elixir? In phoenix you typically have your “context“ modules that act as something like a service layer for one domain (e.g. User/Article/…), and as far as I am aware, it is common to just call functions from other context modules inside each other.

Coming from languages that have things like interfaces and such, that seems a bit messy and lacks clear domain boundaries. I’m not saying that the way java/typescript do it is better/more correct…

In those languages, I would do something like:

When designing something like a user domain, one can define interfaces that this domain needs… and then use Inversion of Control (dependency injection) to satisfy those dependencies…
E.g., an article domain would need some sort of interface that enables it to load user information…

This makes it clear which capabilities each domain needs to function… and you have a clear overview of the dependencies of that domain. So if, at some point, you would need to move a domain into its own service/application, you would have a clear interface to work with.

How is this done in Elixir? How does one structure elixir applications so that they don’t become a tangled mess where everything is using everything and there are no clear boundaries?

I mean, obviously, there are big elixir projects and somehow they are maintained, so I guess there is some way to manage that… it is probably just a different one from the one with the programming languages I’m familiar with/

It would be great if someone could provide some clarity or could point me to some resources to get a better understanding of architecting Elixir applications

Thanks :slight_smile:

Most Liked

LostKobrakai

LostKobrakai

For inversion of control you’d look at behaviours or protocols in elixir. “Behaviours” switch out behaviour by referencing different modules and protocols switch out behaviour depending on the datatype used. Generally people in elixir are quite pragmatic though and use those when there’s actually the need to switch out behaviour. If there’s just a single way to load users you might just use it.

riebeekn

riebeekn

Never used it but in addition to Boundary there is also this which looks interesting https://www.curiosum.com/blog/introducing-contexted

FlyingNoodle

FlyingNoodle

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement