Skip to content

Commit

Permalink
Add new entry to context FAQ
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 21, 2023
1 parent 26e2e5e commit afab0cb
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion guides/contexts.md
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 923,7 @@ defmodule HelloWeb.CartHTML do
use HelloWeb, :html

alias Hello.ShoppingCart

embed_templates "cart_html/*"

def currency_to_str(%Decimal{} = val), do: "$#{Decimal.round(val, 2)}"
Expand Down Expand Up @@ -1348,6 1348,16 @@ Great work!

## FAQ

### How do I structure code inside contexts?

You may wonder how to organize the code inside contexts. For example, should you define a module for changesets (such as ProductChangesets) and another module for queries (such as ProductQueries)?

One important benefit of contexts is that this decision does not matter much. The context is your public API, the other modules are private. Contexts isolate these modules into small groups so the surface area of your application is the context and not _all of your code_.

So while you and your team could establish patterns for organizing these private modules, it is also our opinion it is completely fine for them to be different. The major focus should be on how the contexts are defined and how they interact with each other (and with your web application).

Think about it as a well-kept neighbourhood. Your contexts are houses, you want to keep them well-preserved, well-connected, etc. Inside the houses, they may all be a little bit different, and that's fine.

### Returning Ecto structures from context APIs

As we explored the context API, you might have wondered:
Expand Down

0 comments on commit afab0cb

Please sign in to comment.