Created
April 3, 2019 05:32
-
-
Save sleepiecappy/79bd4f52c483f1da51ae1bdd43521566 to your computer and use it in GitHub Desktop.
Use behaviours to declare interfaces and decouple implementations from business logic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Project.ActiveUsers do | |
@behaviour Project.UseCase | |
def run(params, deps) do | |
deps[:repo].fetch_users(params) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule ProjectWeb.Controller do | |
def index(conn, params) do | |
{:ok, users} = Project.ActiveUsers.run(params, repo: Project.StaticUserRepository) | |
conn.assing(:users, users) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Project.StaticUserRepository do | |
@behaviour Project.UserRepository | |
def fetch_users(_params) do | |
users = ["Joao", "Pedro"] | |
{:ok, users} | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Project.UseCase do | |
@callback run(params :: map(), dependencies :: Keyword.t()) :: {:ok, any()} | {:error, any()} | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Project.UserRepository do | |
@callback fetch_users(params :: map()) :: {:ok, list()} | {:error, any()} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment