cvh23

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)
        :

But what about an Elixir structure like this:

  • shop
    • lib
      • customers
      • payment
      • communication
      • api (all the web/api stuff)
        :

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 :wink:

Showing Posts 1 to 10

Eiji

Eiji

@cvh23 You really need to learn lots of Phoenix stuff. Many of your questions are from someone who never tried them - especially Repo naming which is included in every new phoenix project (mix phx.new generator). Please read official Phoenix documentation. After your first Phoenix app if your code would be really big you may consider refactoring your app into umbrella app.

Some useful resources:

https://elixir-lang.org/getting-started/mix-otp/dependencies-and-umbrella-projects.html

cvh23

cvh23 OP

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

Eiji

This is a template for phoenix projects. Nobody forces you to use it, but it’s a useful resource for generic use cases. It’s made by Phoenix core developers, so those are recommended. Hover using them because they were in some template isn’t enough. For example by default every phoenix project have PostgreSQL database. Depends on your project requirements some part of such code may change, but it’s mostly something like database configuration (like credentials) rather than whole application.

For new Elixir/Phoenix developers such template is more than enough as Phoenix is feature-rich framework (which in fact works like library). In order to develop applications well you should know concept like what are contexts and why we write them. All of this you can find in official documentation, ElixirConf videos and many, many more learning resources. Make sure you look also at Learning Resources category.

hlx

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

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.

lib/
  invoices/
    controller_tests/
      index_test.exs
      create_test.exs
      update_test.exs
      delete_test.exs
    controller.ex
    view.ex
    view_test.exs
    schema.ex
    schema_test.exs
    jobs/
     send_invoices.ex
     send_invoices_test.exs
  payments/
    controller_tests/…
    schema.ex
    …
  customers/
    …

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 :slight_smile:

32
Post #6
cvh23

cvh23 OP

At the moment fun, but with the perspective of profit :wink: 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

cvh23 OP

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

hlx

In that case just start hacking :wink:

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

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

atomkirk

not at all! its awesome. In sublime or vscode I just do invoi/sche to get to invoice schema, etc. so its just the opposite of what you’d normally do mod/inv to 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)

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews