Rockson

Rockson

Context / for request state equivalent

Hi I’m an absolute beginner trying to understand how to achieve the same thing I do with object oriented frameworks.
I can’t understand which is the recommended way to load some data at the start of the request that I can use later in my code without re-elaborating it every time. For example the authenticated user. I load it at the begin of the request, place it in a global store(aka context) and I can access it wherever I want since it is global.

Do I have to pass it using parameters? I don’t like the thing of polluting functions with it.
What does phoenix|elixir recommend?

Marked As Solved

gregvaughn

gregvaughn

I think what you want to understand is the conn struct provided by Plug, which Phoenix builds atop. Plugs and plug pipelines can be used in your router to do things like authenticate a user and store that user in the conn struct so that once you resolve a particular controller/action you can access it.

However, you are expected to pass that sort of thing into functions lower-level in your business logic (Phoenix calls them “contexts”) where appropriate.

Also Liked

gregvaughn

gregvaughn

I would offer to you the argument that is is not “pollution” for a function to accept a user parameter if it depends upon that user to perform its … function properly. That is called functional programming.

There are ways around it, but no, they are not common. What’s common is to have pure functions that take in all the params they need to achieve their function. Those are really easy to write tests for.

gregvaughn

gregvaughn

A Repo is a GenServer. There’s macro sugar to make this less obvious. GenServers can have state. In this case there is a connection pool involved.

kokolegorille

kokolegorille

Remember there are no objects, and FP does not share the same design patterns.

FP is about having pure functions. Everything is about (pure) composable functions…

Where Next?

Popular in Questions Top

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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
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
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement