stevegrossi / habits

An Elixir/Phoenix app for tracking daily habits, ready to deploy to Heroku.
61 stars 7 forks source link

assoc vs. ids: should contexts reach into other contexts? #37

Open stevegrossi opened 5 years ago

stevegrossi commented 5 years ago

assoc

def list_habits(%Account{} = account) do
  account
  |> assoc(:habits)
  |> Repo.all()
end

ids

def list_habits(account_id) when is_integer(account_id) do
  account
  |> where(account_id: ^account_id)
  |> Repo.all()
end

In-Context associations?

Inspired by this forum post.

def list_habits(account_id) when is_integer(account_id) do
  %Habits.Account{id: account_id} # not Accounts.Account
  |> assoc(:habits)
  |> Repo.all()
end

# which requires

defmodule Habits.Habits.Account do
  use Ecto.Schema

  schema "accounts" do
    has_many(:habits, Habit)
  end
end
stevegrossi commented 5 years ago

I'm very curious about the "In-Context associations?" option, which seems to fulfill the promise of bounded contexts (though I know Phoenix contexts aren't explicitly an implementation of DDD's bounded context pattern). In that direction, it would be interesting to have a rule that no context may reference modules within another context. I like that idea in theory, I'm just curious how it would play out in practice.

stevegrossi commented 5 years ago

This post details an interesting approach similar to "In-Context associations", but instead of only passing IDs between contexts, you can also pass _map_s (but not structs, which are domain-specific) between contexts.