deby22

deby22

DDD Elixir/Phoenix registration form with many bounded context fields

Mayby its not a Elixir/Phoenix problem, but i have it in Elixir project, so i think this is right place for this question.

So. I have a project build from 3 services. All build on Elixir/Phoenix
Frontend → just for test. In the future will be rewritten to React probably
Auth → module for authenticate. based on guardian
Core → whole system. Including REST API and Console Admin.

And now i have few question.
First one is how to deal with multi services data. I mean for example register form. One of required field is for example your favourite food. Another is your weight. That fields doesn’t fit to Auth module.

So i decided to build system like this
Front (html) send request to Front Controller
Front Controller send request via Tesla to Core Service
Core Service prevalidate fields and then send request to Auth Service
Auth Service validate own fields (like password), and if everything is OK, save new user and send back info do Core Service.
Corve Service save other fields including uuid given from the Auth system

Is this correct way to to this?

Front --> Core(pre validation) --> Auth(validation and save) --> back to Care(save) --> back to Front with succes message.

There is few issue. First one, how to deal with validation of another system. For example password field. Core doesnt have to know anything about password. But if i don’t validate this field user won’t know that password is invalid unitl i ask Auth for it.

Another solution is send request from front to both services. But what if one of them have invalid data? It should like below?

# register_controller.ex
  def register(conn, params) do
    with  {:ok, validated_client_params} <- API.Core.validate(params),
            {:ok, validated_user_params} <- API.Auth.Core.validate(params),
            {:ok, client} <- API.Core.create_client(validated_client_params),
            {:ok, user} <- API.Auth.create_client(validated_user_params) do
            render("submit.html", client: client, user: user)
    end
  end

But there is 4 request to do simple thing like create new user. SLA of this solution is terrible.


Second question is related of this topic.
What if i have many fields on form but they are from different bounded_context in my backend?
Like i said before i have AUTH like another service (security policy)
In Core i have few bounded context.

  • Client for basic information about client
  • Limits for restrict acces to resources
  • Foods for food catalog
  • Diet for create new diet plan based on foods and related to clients or dietician.

So how should i implement it if on register form i have to put field from diet context, like → prefered diet, or mentioned favourite food, to better target diet advertisement?

I’m thinking about register scope/controller/view. This is only virtual scope. By virtual i mean only in web parts. This concept won’t appear in my contexts.

It could looks like this

# web/api/register.ex
  def register_controller(conn, params) do
     with {:ok, client} <- Clients.create(params),
             {:ok, food_preferences} <- Foods.create_if_not_exist(params),
             {:ok, diet_mentor} <- Diet.related_dietician_mentor(params),
             {:ok, diet_preferences} <- Diet.create_diet_preferences(params) do
                conn |> ...
   end
 end

# clients.ex
def create(params), do: Client.changeset(params) |> Repo.insert()

# foods.ex
def create_if_not_exist(params), do: ...

If anyone need to change the same field in the future i have to add another controller but now in separated bounded_context. registration context is just for keep whole process consist and isolatet from external word. I mean Front apps shouldn’t know which fields are related to which bounded context. Core could save all of this fields to keep “copy” but source of true is bounded context which update state.

Thanks for any advice!
Mayby there is easier way to do that?

Any good books/wideos to read/watch. On Elixir ofc.

I watched this wideo twice: https://youtu.be/l3VgbSgo71E
I think its pretty good, but still i don’t know how to deal with this concept in practice :expressionless:

Where Next?

Popular in Discussions Top

jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
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
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
slashdotdash
Phoenix Live View is now publicly available on GitHub. Here’s Chris McCord’s tweet announcing making it public.
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement