At Novistore we're building an awesome e-commerce API and have a lot of schemas in our project. To wrap all that up we use contexts as introduced in Phoenix. Contexts are great, but they can really grow in size. Devon wrote a great article about one of the ways to solve it, but after trying it out we decided to take another route.
This is a small example of how our contexts look like:
defmodule MyApp.Catalogs do use Context, repo: MyApp.Repo context MyApp.Catalogs.Product context MyApp.Catalogs.Collection, only: [create_collection: 1, delete_collection: 1] def list_products do # list all products end end
defmodule MyApp.Sales do use Context, repo: MyApp.Repo context MyApp.Sales.Checkout context MyApp.Sales.Order, except: [delete_order: 1] def list_orders do # list all orders end end
context/2 macros make sure that a lot of the standard functions used to interact with the schemas are created at compile-time. This makes our contexts a bit smaller and easier to work with.
context MyApp.Catalogs.Product creates the following functions:
context macro also supports options like
only:  and
except:  to make sure you don't generate functions that you don't need.
We hope you like it and you'll benefit from it too. We decided not to package this into a library because some functions might/might not be needed for your project.
Below is the code to make it all work:
Edit: For some reason the code snippet displays
@\__repo_\_ incorrectly, so I've moved the context code to a gist: https://gist.github.com/hl/c535b594b24e858d0e5ccfff29280c7a