Importing a behaviour definition (and just it) in a library

Hi all,

I’m working on a Phoenix project (Asteroid) in which I define some behaviours such as Asteroid.TokenStore.AccessToken. I want to have different implementations in separate libraries, such as

  • AsteroidTokenStoreAccessTokenMnesia
  • AsteroidTokenStoreAccessTokenRiak

My question is: what’s the best practice to import Asteroid.TokenStore.AccessToken (in lib/token_store/access_token.ex) in my separate libs?

I’ve tried with dependencies:

defmodule AsteroidTokenStoreAccessTokenRiak.MixProject do
defp deps do
      {:asteroid, path: "../asteroid", runtime: false},

However, this approach causes 2 problems:

  • it imports the whole Phoenix project, dependencies and compiles them all - this is not wanted
  • side effect: this results in endpoint.ex of the Phoenix project not compiling because it cannot find Poison which is not loaded (and the configuration config :phoenix, :json_library, Jason in my Phoenix project is lost)


You cannot do that. If you want to share that behaviours definitions then you need to extract the code to separate library.

1 Like

@hauleth is correct. I want to also caution you against premature abstraction. If you try to do all the decoupling steps now while you really don’t even know how to do it, you are going to make your project more complex for yourself. I encourage you to write the application that you need to write. Later, after you’ve used and lived with the code for a while, then look for seams where it might make sense to extract a library.


Thanks for your answers.

Extracting the code seemed to be the way to go, but while coding I realized that this behaviour uses types and returns structs that are in other modules of the Phoenix project, and moving them also would result in extracting a important amount of modules (not sure where it’d stop).

Wouldn’t I be looking for something similar to C/C++/Erlang header file includes? Afaik there is no such thing in Elixir.

I think I’ll merge it in the core app as suggested, even though I’m not comfortable to include riak, redis, etc. as dependencies when not needed (but I guess runtime: false in mix.exs will do the the trick). I’ll post back for suggestions when I’ve some code to show.


1 Like

Now that I have some code to show, here’s an example. The goal is to allow using a behaviour in separate libraries (eg. : a Redis implementation of an access token store). The access token store behaviour of Asteroid refers to type in other modules:

defmodule Asteroid.TokenStore.AccessToken do


  @callback get_from_subject_id(, opts()) ::
  {:ok, []} | {:error, any()}


At that point I see 2 options:

  • Extract these types altogether with the behaviour to external libs (why not a type lib)
  • Not using these and types but some more generic ones such as String.t() and extract only the behaviour

What do you think?