sammkj

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

  • Accounts
    • register_user
    • login_user
    • update_user_profile
  • Merchants
    • register_merchant
    • update_merchant_profile

Option 2

  • Accounts
    • register_user
    • register_merchant
  • Authentication
    • login_user
  • Profiles
    • update_user_profile
    • update_merchant_profile

or maybe other options?

Most Liked

AstonJ

AstonJ

Also check out these threads:

:023:

wmnnd

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

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

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.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement