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.

Last Post!

dimitarvp

dimitarvp

Mostly yes but don’t get religious about it. It’s okay for your business logic functions to depend on Ecto and Plug which have stateful (impure) parts. Utilise some tooling (like the boundaries library by @sasajuric) to check for dependencies you don’t want to allow but outside of that, don’t sweat too much about it. :slight_smile:

Where Next?

Popular in Discussions Top

New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
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
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
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
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement