sammkj
Need help understanding Phoenix Contexts
I’m a bit confused about what context really is after reading the Phoenix Context Guide. From the guide, it seems like Accounts context deals with everything related to users (registration, authentication, etc.). But aren’t those supposed to be its own contexts? So which of the following is the right way to define context?
Option 1
Accountsregister_userlogin_userupdate_user_profile
Merchantsregister_merchantupdate_merchant_profile
Option 2
Accountsregister_userregister_merchant
Authenticationlogin_user
Profilesupdate_user_profileupdate_merchant_profile
or maybe other options?
Most Liked
AstonJ
wmnnd
You’re right, depending on the structure of your application it might make sense to draw different lines between the contexts. Since it is just a question of how you want to organize your application, it doesn’t really matter as long as the way you split your contexts is reasonable and understandable.
In the example you give, I would also say: It depends.
It depends on what the individual functions do. I. e.: Are Merchants also Users? Does Merchant use a different schema than User? Answering those questions might help you decide which functions belong to the same context and which don’t.
EssenceOfChaos
@sammkj - There is no “right” or “wrong” way to build your application. It ultimately comes down to whatever makes the most sense to you for your specific use case. You’re already on the right track by considering your application structure beforehand, a benefit of Phoenix’s contexts.
As to your question regarding the two options, the second option does look better. The contexts are simply a formality that encourage name-spacing and allow you to write decoupled code. By starting to develop with contexts in mind you’re setting yourself up for easier refactoring in the future. So, if you should choose option 1 or 2 today and in the future change your mind, you’ll be able to restructure the application without much hassle.
Last Post!
LostKobrakai
To be honest I feel this is putting to much into the idea of phoenix contexts. While phoenix had to come of with some form of contexts to be able to provide generators for them I feel the idea is much broader: Do not put business logic in your web layer, but create an plain elixir setup of modules with functions for it.
Not being more concrete allows for everyone doing phoenix to decide what makes most sense for their project. People can go a hardcode hexagonal arch. route with all sorts of layers and abstractions or have a single core context (when the business domain is not to complex). It however also has the downside of not being super obvious to beginners, as they cannot be guided to a one-size-fits-all solution and possibly need external/additional knowledge.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex










