henriquefernandez

henriquefernandez

UseCase - A way to increase Elixir projects readability and maintenance based on "use cases" and "interactors"

A way to increase Elixir projects readability and maintenance based on use cases and interactors , the main goals are:

  • Single Responsability Principle
  • Composability
  • Readability
  • Screaming architecture
  • Enforce inputs and outputs of our project use cases
  • Better errors (know exactly where code fails)
  • Standardization

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hi @henriquefernandez this is definitely an interesting idea. I think a part of getting people to adopt something like this is providing examples that speak to more realistic or comprehensive examples. How would this look like with phoenix / ecto? How would this look like with several distinct tables?

al2o3cr

al2o3cr

IMO this needs work - the macro declares an input struct for the use case, but in a UseCase.pipe situation the input isn’t actually that shape (it’s the *.Output struct from the preceding step).

AFAIK the style of indirect invocation used here is going to make it really hard for tools like Dialyzer to spot mismatches between inputs and outputs - they’ll only be detected at runtime. Consider enhancing things like pipe to enforce compatibility at compile-time.

Re: UseCase.pipe, the asymmetry in the argument list (struct followed by N atoms) feels weird. It also doesn’t seem particularly friendly to composition; is there a general way to take

UseCase.pipe [%A{arg1: "foo"}, B]

and then pipe the result into a third use case C?

henriquefernandez

henriquefernandez

Hi @benwilson512, thank you very much for the tip!

I made an improvement in the README and I intend to keep improving over time :grinning:

Last Post!

henriquefernandez

henriquefernandez

Hi @al2o3cr, thank you for the tips.

IMO this needs work - the macro declares an input struct for the use case, but in a UseCase.pipe situation the input isn’t actually that shape (it’s the *.Output struct from the preceding step).

We can solve it pattern matching the interactor call function to receive its inputs or an another interactor output. In that way we are explicitly declaring that this interactor can be composed with another one in a specific way :slightly_smiling_face:

Despite the ideas of forcing inputs and outputs, I tried to make the library as malleable as possible. We can use it in the way that suits us best.

AFAIK the style of indirect invocation used here is going to make it really hard for tools like Dialyzer to spot mismatches between inputs and outputs - they’ll only be detected at runtime. Consider enhancing things like pipe to enforce compatibility at compile-time.

I will study about it for new versions, thank you.

UseCase.pipe , the asymmetry in the argument list (struct followed by N atoms) feels weird. It also doesn’t seem particularly friendly to composition;

I did an update on it, now we can do that:

%A{name: "Henrique"} 
|> UseCase.pipe [A, B, C]
|> UseCase.pipe [D]

is there a general way to take
UseCase.pipe [%A{arg1: "foo"}, B]
and then pipe the result into a third use case C ?

I don’t know if I understood it very well, but we can do this:

UseCase.pipe [%A{arg1: "foo"}, B, C]

I really appreciate your comments! Thanks!

Where Next?

Popular in Discussions Top

andre1sk
A big advantage to Elixir is all the distributed goodness but for many applications running on multiple nodes having integrated Etcd, Zoo...
New
Jayshua
I recently came across the javascript library htmx. It reminded me a lot of liveview so I thought the community here might be interested....
New
jswny
I would like to better understand what the advantages/disadvantages of umbrella applications are compared to structuring your app as as s...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
jeramyRR
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
Qqwy
I would like to spark a discussion about the static access operator: .. For whom does not know: it is used in Elixir to access fields of...
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43657 311
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127536 1222
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