lucianonooijen

lucianonooijen

Interactors in Elixir following Clean Architecture guidelines

At the moment I am creating a plan to integrate Elixir at enterprise scale for several clients of the company I work for.

Part of this integration would mean the application should comply with the guidelines as outlined in Uncle Bob’s Clean Architecture. (A tldr version can be found here: Clean Coder Blog)

The part I am currently stuggling with is using Interactors in Elixir, without the use of any libraries.

How Interactors are usually implemented is by creating an Interactor interface (with all available functions/methods). The business-logic core expects an object where this Interactor is implemented, so the business-logic core can be fully agnostic of data sources and such.

An example of an interactor based design in Go can be found here: go-realworld-clean/uc/userGet.go at master · err0r500/go-realworld-clean · GitHub

As far as I know there is no easy way to define an interface in Elixir, the way you’d usually do in languages like Go, Typescript, Java or even Scala. I was not able to find any good examples where this kind of architecture is implemented in Elixir.

Using typespecs/behaviours/protocols seems to make the code become quite messy if the Interactors grow in size, so I am looking for a way to implement this architecture while keeping the code clean and easy to maintain.

Thanks a lot in advance!

Marked As Solved

LostKobrakai

LostKobrakai

You seems to be stuck at “type safety” which just doesn’t exist on the beam. There is no type safety. But GenServers work solely on the idea of a callback module being passed in and it’s basically the backbone behind most of everything running on the beam and it works well even without the type safety. You can have tests ensuring that a certain module conforms to a certain behaviour. You can use whitelists of some kind to allow just tested modules as inputs. If you really want to prevent someone passing in a wrong module there are ways, but not via types. I’d rather suggest making your app handle failure properly and weed out those bugs when they actually happen.

Also Liked

LostKobrakai

LostKobrakai

You can’t. I just answered this on slack a few hours ago. I’ll just paste this here:

  • there’s only module() as built-in type or
defmodule MyBehaviour do
@type t :: module()
end
  • and MyBehaviour.t to at least be a bit more concrete for humans

  • also it’s not really a good fit for dialyzer, which will only error if it can be certain about a failure. Modules can be loaded at runtime, so there’s no way for dialyzer to check callbacks in advance

LostKobrakai

LostKobrakai

There’s dialyzer, which can do type checks, but it works considerably different to e.g. typescript. Bu even typescript would have problems with checking if a certain class implements an interface if the class could be created at runtime out of nowhere.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

We're in Beta

About us Mission Statement