tjdam

tjdam

How independent should contexts be?
A more particular question: say I’m writing a blog and I want to make sure no posts are persisted in the database without a user tied to it. Where exactly do I make this check to make sure that always happen?

I’m currently doing this kind of checks in the context functions, and now that I’m increasing the number of contexts in my application I find myself importing a lot of the application just to write context tests, which gives me the feeling that I’m doing it wrong and that there might be a better way to do it.

Any tips on that one?
Thanks :slight_smile:

Showing Posts 1 to 5

andreapavoni

andreapavoni

As a general introduction, contexts are used to expose a public API while hiding the implementation details that aren’t necessary.

That said, it’s common to call some functions from another context while being into one, how or when to do this depends by what you’re doing, hence your domain.

You can also use a dedicated context to glue other ones into a single place and expose inly the functionalities at an higher level. Again, how or when you do this depends by your domain.

When testing a context, you could mock functions from the external contexts.

Hope this helps a bit to clarify :wink:

Edit, more details: regarding your question, if you have Accounts and Blog contexts, and you want Post to have an existing user, at least you can check that user_id is present. If you want a stricter check you can either 1) do a manual check by querying Accounts for the given user_id and see if it exists, or 2) create a database constraint (which breaks some isolation under the hood), or 3) replicate user data on Blog context (a bit overkill in this specific use case).

Qqwy

Qqwy

TypeCheck Core Team

I believe that in many cases, as long as you are able to draw out your dependencies as a directed acyclic graph (in other words: there are no cycles, no contexts that both depend on each-other), then you are usually fine.

tjdam

tjdam OP

I think my main issue is seeing how the chain of the dependencies can grow big and make test really weird sometimes.

In this same example, say I created a check to verify that a user is always present in a blog post, but then user itself has its own associations it depends on. There already would have me bringing two other contexts to this just for testing!

LostKobrakai

LostKobrakai

Very much this. Your context are unlikely all on the same level of abstraction, but you should always only depend on something of a lower level of abstraction. On recent elixir versions I’d try to get Boundary setup, which can help with such things and also bring more visibility in the otherwise hidden-in-implementation-details tree of dependencies.

tjdam

tjdam OP

Ohh, that looks neat..!

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
Stef-Verniers
Hello I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews