How to use dependency injection pattern in Elixir?

Tags: #<Tag:0x00007f8ead601538>


Just to follow up, I’ll give here the same example for this I gave in Slack.

We can define a protocol for our use case - e.g. a repository of users:

defprotocol UserRepository do
  def get(database, id)

And then we can define two implementations - one based on a real database and one based on a simple in-memory ETS store:

defmodule Database do
  defstruct [:ecto_repo]

  def new(repo), do: %__MODULE__{ecto_repo: repo}

defmodule EtsDatabse do
  defstruct [:table]

  def new(), do: %__MODULE__{table:, [:set, :public])}

We can then implement the protocol for those data structures

defimpl UserRepository, for: Database do
  def get(%{ecto_repo: repo}, id) do
    repo.get(User, id)

defimpl UserRepository, for; EtsDatabase do
 def get(%{table: table}, id) do
  # assuming {{User, id}, struct} tuples in ets table
  :ets.lookup_element(table, {User, id}, 2)

That’s a simple sketch of a protocol-based dependency injection.


I was preparing a message to answer with your example (and the obligatory mention + thank you) but you beat me to it. Core team members are unbelievable :smiley:


It makes perfect sense to me, however I now wonder if I should keep using behaviours for this purpose. This looks nicer and more reliable. What’s the rule of thumb here @michalmuskala ?