ssbb

ssbb

How to determine contexts with Phoenix 1.3

I am just trying to migrate my project to Phoenix 1.3 with new contexts mechanism. But I am not sure how I should split my schemas into this contexts. I read a lot about DDD and UL/Bounded Contexts in last few days, but all examples using something like large ERP systems. Also in DDD contexts used to “namespace” UL (ubiquitous language) within this contexts like Sales/Support/Analytics, etc. where Product will be used.

At other side @chrismccord used blog (post, comments) example in his ElixirConf talk. So it looks more like module organization (maybe like Django re-usable apps) and not business model separation to scope UL within this context. So how to know when bounded context should be used?

Whats if I have pretty simple project where I have no overlaps in UL? How to scale DDD down in this case? Maybe just use single context?

Should I think about contexts as module organization - Blog, Accounts, etc. or it is really DDD contexts like Sales/Support? I am confused when they are used together (at single ElixirConf talk by @chrismccord).

Most Liked

chrismccord

chrismccord

Creator of Phoenix

Contexts are literally well named modules and functions, and we ask you to think about the API boundaries are you are building your well-named modules an functions. That’s it. If you think that’s a bad idea, then you are saying the way we all build functional programs is a bad idea :slight_smile:

When you build a phoenix project, it literally is an “OTP application”. This conflation w/ the above tweet ignores a lot of details about the elixir and ruby worlds. In Rails, your project is a special directory structure tailored to a Rails application. The entire program is started and stopped according to special rails init code. Contrast that with the way we build Elixir applications, they are all OTP applications, with standard start/stop mechanisms and explicit application relationships. “Phoenix is not your application” absolutely applies here and pushes folks to think differently about Elixir as a platform, since it’s unlike what they’ve been exposed to before.

Baby-steps! :slight_smile: This is exactly what mix phx.new my_app --umbrella does. [quote=“sync08, post:15, topic:4367”]
If I am wrong then please tell me. I realize that I sound very negative but I assure you that I’m very open to ideas.
[/quote]

You are wrong :stuck_out_tongue: . Thanks for the feedback!

josevalim

josevalim

Creator of Elixir

One more comment on this.

First of all, Elixir is not Ruby and Phoenix is not Rails. Ruby, opposite to Elixir, does not provide any mechanism for structuring your projects. Most of the standards you see today in Ruby came from the community.

In Elixir, we do have such structure. Elixir itself has the concept of applications and how code is structured. Those are pushed as part of the language and its tooling.

When we say Phoenix is not your application, it is not some anti-Rails instance. So don’t make it so. It is literally how writing projects work in Elixir (and Erlang/OTP). We had technical reasons for generating the “web” directory in previous versions but those have been solved, so Phoenix projects should follow the standards in the community instead of fragmenting it.

The way we are advocating Phoenix applications to be structured is exactly how community projects such as Elixir itself, Phoenix and Ecto are structured. In Ecto, we have “contexts”, such as Ecto.Repo, with private modules inside, such as Ecto.Repo.Supervisor. Only Ecto.Repo is allowed to talk to Ecto.Repo.Supervisor. Sometimes we do violate this rule by making an inner module public but that’s exactly the point: building a project with Phoenix should be no different from building any other Elixir app.

sync08

sync08

The more I read about contexts the more I think it’s a bad idea.There seems to be a big push for “Phoenix is not your application” which, as far as I can tell, is nonsense. It’s a very big anti-rails movement.

Phoenix is your application. It’s a framework that serves as the foundation which you build on top of.

Ecto, Plug e.t.c. are libraries which are not your application. You use them as libraries and as such they don’t impose any structure. You’re free to do use them as you please.

A relevant quote from dhh.

https://twitter.com/dhh/status/218740829806792704

It doesn’t help that the two main people behind Phoenix and these changes propose this complex new structure and both reply with the same “oh no we can’t give examples because it all depends on what you’re building.” I don’t mean to sound too critical but this screams change for the sake of changing.

What we have now is a situation where we have multiple isolated contexts in the same app but are discouraged from actually using them as a whole. e.g. we shouldn’t have relationships to other contexts in schemas. This is pretty backwards because if you have these in the same app you are certainly going to want to use everything together.

I personally don’t think that contexts is the right solution. I also don’t think we actually had a problem in the first place. If you really need separate isolated contexts then they don’t even belong in the same app.

Contexts as they are now are a bastardization of DDD and the repository pattern.

Umbrella apps are where the attention should have been focused. Fully separate apps where necessary and a web app that ties them all together.

If I am wrong then please tell me. I realize that I sound very negative but I assure you that I’m very open to ideas.

Where Next?

Popular in Questions Top

fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement