Devon estes Context rule suggestion

I came around this blog post which deals with contexts. I really like the idea. But being new to elixir and Pheonix my biggest hardship is… Naming and placing files correctly.

His main takeaway is to have 3 boundries to every datasource:

  1. the schema file - Which only holds the Schema and the ChangeSet
  2. A Secondary Context - Which only responsible for the basic CRUD operations and is the only module allowed to call the Repo for the specific Schema
  3. A Primary Context - the actual modeling of particular relevant business use-cases and flows.

So he propose this structure:

├── social_media.ex
└── social_media
    ├── users.ex
    ├── users
    │   └── user.ex
    ├── facebook_posts.ex
    ├── facebook_posts
    │   └── facebook_post.ex
    ├── twitter_posts.ex
    └── twitter_posts
        └── twitter_post.ex

Now lets assume another primary context: Accounts

├── accounts.ex
└── accounts
    ├── users.ex
    ├── users
    │   └── user.ex

In this scenario, we are using the same table ‘users’.

And my question is this: is accounts/users/user.ex the same as social_media/users/user.ex?
and if so, isn’t this pointless duplication?

I am sure he didn’t even fathom this as an option so I ask you, oh experienced Pheonixers:

Should there be a Schema module for all schemas? and then a secondary Users context for the CRUD actions and then Primary contexts that have to do with Users use that context when modeling use-cases?

And if so, how would you structure the folders?


Not necessarily. For example, the official phoenix contexts guide creates a new table to store post authors instead of using the existing users.

Counterintuitive at first. But makes real sense in the long run. From the application maintenance perspective.