How would you explain Phoenix Contexts to a newbie?

Here’s a concise explanation I’ve had some success with before:

The console (iex) can/should be thought of as a 2nd UI layer of parallel importance to the web UI. If complex business logic is only in controllers, then you have to duplicate that in the console. If complex business logic is in context modules, then you get to call them and reuse that logic from your console UI layer.

Contexts act as that reusable layer for business logic. The names of modules/functions/parameters should reflect business domain concepts.

21 Likes