jarrodm

jarrodm

Avoiding a dependency cycle when using Router.Helpers

Hi Phoenix ppl,

I have a separate notifications service, and for some time now I’ve simply been pushing all the arguments it needs over to the service including the URLs it needs.

It simplifies dependencies, but it means that the web service had to gather all the information needed according to the whims of whatever the notification templates needed. It also meant that the data could be stale by the time the notification was rendered, and this arrangement has fallen apart now that I want some notifications to be polled instead of triggered.

Recently I’ve refactored to the notification service to pull the information it needs from the database, but it also needs to generate the URLs. This causes a cycle with the business logic in the web service needing to trigger new notifications, but also the notification service needing access to the business logic, data model and router in the web service.

My solution to this is to trigger the notifications service at the API/controller level (probably where it should have been all along), and to move the business logic and data model out to its own dependency. But I think I also need to clone the url helpers of the router as it depends on the controller creating another cycle – right?

It’s not a huge deal, as any inconsistencies between the two could be caught in unit tests, but the redundancy does seem like a place for bugs to emerge… and there might be a better solution?

I’m thinking this is a common problem as projects grow, and I’d be keen to hear how other people have tackled this problem. Thank you.

First Post!

jarrodm

jarrodm

I feel like I’ve failed to communicate the problem, so I’ll give it one more shot! Has anyone wanted to share their router paths with another application, which the web server depends on? If so, how did you avoid the dependency cycle? Can it be done without duplicating functionality of the router?

Most Liked

LostKobrakai

LostKobrakai

It doesn‘t really matter too much what you do and when. If BusinessApp is supposed to be decoupled from WebApp, but also needs to know details only WebApp is aware of (like urls), then one solution would be to provide these details whenever WebApp calls api of BusinessApp. Something like BusinessApp.start_registration_flow(user, WebApp.business_routes()). That way BusinessApp doesn‘t need to call into WebApp again to get to know those urls.

fuelen

fuelen

Just created MyApp.Links module which contains URLs I need. Typically, you don’t need a lot of URLs, and it is not a big pain to support duplicated functionality.

LostKobrakai

LostKobrakai

Your controller could pass in all the urls needed by „Business Logic/Repo“. That way there‘s no nedd for the dependency back into the web service.

Last Post!

fuelen

fuelen

It is not always that simple, as URLs are usually parameterized, and with this approach there may be a need to store them in some DB in order to be processed later.

Where Next?

Popular in Questions Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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
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 44608 311
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

We're in Beta

About us Mission Statement