cvh23
Hey,
I am very new to the Elixir world, but doing software development for many years. The Elixir ecosystem looks very promising to me. Currently I am not sure how to structure a larger application in an optimal way. Let’s say I want to create a web shop application named “shop”, which is an API only (REST or GraphQL) application. I would structure it this way (disregarded Elixir):
- shop
- customers
- payment
- paypal
- stripe
:
- communication
:
.
With Elixir (Phoenix) it would be?
- shop
- lib
- shop
- customers
- payment
- paypal
- stripe
- communication
:
- shop_web (all the web/api stuff)
:
- shop
- lib
But what about an Elixir structure like this:
- shop
- lib
- customers
- payment
- communication
- api (all the web/api stuff)
:
- lib
That would be more “natural” to me (for an API only application). Otherwise I would have repeated “shop” and all the code is split more or less in two main directories: “shop” and “shop_web”
Another question: Is it uncommon to have the application name in the module identifier/path? So it should not be “Shop.Payment.Paypal”, but just “Payment.Paypal”. Right?
How should the Ecto repo modules be named? “Payment.Paypal.Repo”? Or “Payment.Paypal.DB”? Should the Repo be seperated from related business logic?
Sorry for mixing the topics a little bit, but for me it’s somehow connected ![]()
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
@cvh23 You really need to learn lots of
Phoenixstuff. Many of your questions are from someone who never tried them - especiallyReponaming which is included in every newphoenixproject (mix phx.newgenerator). Please read officialPhoenixdocumentation. After your firstPhoenixapp if your code would be really big you may consider refactoring your app intoumbrellaapp.Some useful resources:
https://elixir-lang.org/getting-started/mix-otp/dependencies-and-umbrella-projects.html
cvh23
Thanks @Eiji for the links. I’ve already tried a small project and some of the generators, but I am not sure how obligatory or recommended these settings are.
I’ve also read about umbrella apps, but I thought this approach maybe “too big” for an application which just consists of some parts/modules.
Eiji
This is a template for
phoenixprojects. Nobody forces you to use it, but it’s a useful resource for generic use cases. It’s made byPhoenixcore developers, so those are recommended. Hover using them because they were in some template isn’t enough. For example by default everyphoenixproject havePostgreSQLdatabase. Depends on your project requirements some part of such code may change, but it’s mostly something likedatabaseconfiguration (like credentials) rather than whole application.For new
Elixir/Phoenixdevelopers such template is more than enough asPhoenixis feature-richframework(which in fact works like library). In order to develop applications well you should know concept like what arecontextsand why we write them. All of this you can find in official documentation,ElixirConfvideos and many, many more learning resources. Make sure you look also at Learning Resources category.hlx
One question first, is this for fun or profit?
Fun
Just experiment, take a look at the source code from changelog.com and hex.pm (search on github) and build what you think works for you and post it here for others to look at and help you with. It’s very easy to refactor in Elixir and you’ll learn a ton in the process.
Profit
Just build it with the default settings and ship it. No matter how you structure your app, if you don’t ship it it does not matter.
atomkirk
I’ve worked on a number of big Phoenix/Elixir applications. I led the development of a big one (200k+ lines) over 4 years. We did it both ways, the way phoenix boilerplate does it for about 2 years, and then we reorganized it. The main thing we learned was to organize your project around features, not functionality. Why? Because which is more common? working on all your tests at the same time? or all your controllers, views, or graphql code at the same time? Or, working on a particular feature? Sometimes you do refactor something across an entire category of functionality, but WAY more often you or someone maintaining your code is working within a feature/resource/group of resources. So here’s how we did it, and we never ever looked back. It it insanely amazing and other people who have worked on our project have commented on how much easier it is to work on a feature and find all the relevant modules they need.
We tore a page from golang and we put the tests right by the modules they test. This is so great. Why do we do all the work to build a mirror of our project dir in the test? Tests are code, they are part of our app. This also makes modules that are not tested stand out like a sore thumb. Need to update a module and the test? They are right by each other!
Anyway, I was stuck in analysis paralysis about this, asked the elixir community in slack if I should do this, they all advised against this, but this project has had this structure for 2 years now and I will be doing all projects like this in all languages/frameworks from now on. It is nonsense to organize your project by functionality (controller/view/etc) rather than feature/type/resource. I don’t know rails why ever sent us down that route.
Lastly, when you want to split it up into umbrella apps or microservices, this directly structure is exactly how you’ll want t do it, you’ll just copy the directory wholesale into another app/service/etc. This organization very very much helps facilitate a domain driven design mindset.
I highly highly recommend this
cvh23
At the moment fun, but with the perspective of profit
Just looking how to realize projects with Elixir, which I have already done with TypeScript. For future projects I want to change my backend stack.
Thanks for the hints. Refactoring looks indeed a lot easier than in other ecosystems.
cvh23
Thank you, looks interesting. In general I am also in favor of splitting apps into feature parts and not technical parts. Of course sometimes it makes sense the other way around.
hlx
In that case just start hacking
Not sure if you know but module names have nothing to do with the location of the file on disk. So even if your file is in shop/lib/shop/payments/paypal.ex you can still name the module MyApp.Paypal
hlx
Interesting structure, not sure if I would like it but if it works for you, perfect!
How would you find files in your editor though, would that not be challenging with those filenames?
atomkirk
not at all! its awesome. In sublime or vscode I just do
invoi/scheto get to invoice schema, etc. so its just the opposite of what you’d normally domod/invto fuzzy search for models/invoice.ex. Then, in vscode I do command + E and it shows that file in the sidebar and all the other relevant files I’m going to be working on are right there (tests, graphql resolvers, etc)