Skip to content

Instantly share code, notes, and snippets.

@schrockwell
Created July 12, 2017 18:04
Show Gist options
  • Save schrockwell/96d08965df3bbc562024747be945c489 to your computer and use it in GitHub Desktop.
Save schrockwell/96d08965df3bbc562024747be945c489 to your computer and use it in GitHub Desktop.
How To Build a Scalable Phoenix Application

Don't reference Repo in a controller

Don't do this:

def show(conn, %{"id" => id}) do
  post = Repo.get(Post, id)
  render(conn, "show.html", post: post)
end

Instead, do this:

def show(conn, %{"id" => id}) do
  post = Posts.get(id)
  render(conn, "show.html", post: post)
end

Reasoning

Ecto's Repo is an implementation detail internal to your application that should not be exposed to the web layer. Data persistence can be backed by many things: a relational database, a document database, an ETS table, a flat file, an external API, a GenServer… the list goes on. Leave it up to the Context to hide that detail from your external web interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment