Contexts in Phoenix 1.3 and Facade Pattern

I’m fairly new to Elixir and just started learning about the changes in Phoenix 1.3. The BoundedContext pattern makes sense although at this stage I’m probably looking at a single catchall context.

When I look at the generated scaffold code, the context files reminds me of how I would implement Facade pattern with J2EE in the past. I used to think of the entities (or models) as nouns. I’d never access models directly, rather I’d use the Facade layer. I’d think of the facade as verbs. This also aligned with stateful and stateless EJBs.

example context:

defmodule App.Auth do

  # a verb
  def list_user do
  end
  
  # another verb
  def get_user(id) do
  end

  # more verb!
  def create_user(attrs \\ %{}) do
  end

end

For anything I worked on, this separated things quite well.

  • Models were simple
  • No business logic in the controllers
  • Business logic was always in the facade and well organized
  • Easy to use SQL where needed
  • Avoid n + 1 problem easily

I always missed this approach in most modern MVC frameworks. I’m not sure yet whether I’ll do the same thing, but it looks promising that I can find something similar or better in Phoenix.

Since this is new, I was wonder anyone sees the similarity? How do you use contexts?

4 Likes