What are your "Go to" or favorite patterns, abstractions, and idioms to use in a Phoenix project?

For those of you who have worked on larger Phoenix projects, or those that have worked on more than a few Phoenix projects, what are some abstractions, patterns, and idioms you find yourself re-implementing and re-using regularly?

I am especially interested in idioms for Phoenix, Ecto, and testing.

Here’s an example of something I found interesting. In the Phoenix Guides they describe Contexts as:

A context will group related functionality, such as posts and comments, often encapsulating patterns such as data access and data validation. By using contexts, we decouple and isolate our systems into manageable, independent parts.

However, in the Programming Ecto book their description takes my understanding of Contexts to the next level:

For our own apps that use Ecto, contexts help us divide the pure code from the impure. We recommend putting all impure code (that is, code that uses Repo) into the context module, and putting the pure code (manipulations of changesets, queries, and the like) into schema modules that live in the context’s namespace.

Ecto mentions pure and impure while the Phoenix guide does not. This type of recommendation blew my mind :exploding_head: and now when I look at default (auto-generated) context it’s all right there but I never noticed. I had previously stopped thinking about it as “it’s all related to the context” and didn’t see the other pattern of it being pure vs impure.

I am looking for more nuggets like this.

Thank you

5 Likes

This thread has some nice discussion on a relevant topic: Hexagonal architecture in elixir

3 Likes