smanza

smanza

Struct and complex logic

Hello.
I practiced Elixir for few months now but every time I wonder about some code organisation and particularly with struct and business logic.

I don’t know if struct must only define simple data transformation and validation or should contains more complex and business logic instead of dedicated business logic module.

From one side I have the feeling than logic functions dealing with the struct should be in the same module. But from another side, those functions may requires calls to other business logics modules etc… So maybe the abstraction and responsibility is not well designed.

What do you think that should be belong into struct module ?

Thanks

Most Liked

mbuhot

mbuhot

Yes that’s the approach I follow.
Declare data structures and the functions to transform them in the same module.
Then separately the business use cases in other modules.

Domain Modelling Made Functional is a good reference on organising code with domain models and workflows.

mbuhot

mbuhot

Complex structs like %DateTime{} are a good example where the external users are discouraged from pattern matching directly on the struct fields, and should use the functions provided by the module to transform the value.

For Ecto shemas I like to put the functions that produce changesets in the schema module, then the higher level business logic that manages the transaction scope in context module function, or even a dedicated module for handling each command.

mbuhot

mbuhot

Do you think struct functions must only be pure

For application code, that’s probably a good rule to follow, it aligns with the ‘functional core, imperative shell’ architecture style.

However for library code, there are examples where it doesn’t apply:

Plug.Conn is mostly pure functions, but it also contains the necessary functions like read_body and send_resp which aren’t pure.

Ecto.Changeset has unsafe_validate_unique that makes a DB call while most other validations are pure.

Where Next?

Popular in Discussions Top

PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53578 245
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: Background Some time ago I came across a talk: How we scaled git l...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39247 209
New
IVR
Hi all, I’ve seen a number of related threads in the past, but I’d still be very curious to hear an up-to-date opinion on this topic. I...
New
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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