Fl4m3Ph03n1x

Fl4m3Ph03n1x

Is there an easier way to "Elixirize" this code?

Background

I am reading a book called “Learning Domain Driven Design” (DDD). In this book there are some code samples, usually in C#. Since I am trying to grok these concepts as best as I can, and since I am trying to turn this into material that other people can learn from as well, I took it upon myself to convert the code into Elixir format.

I have on thing in mind only: If you didn’t know about Elixir, would this code be easy to understand?

I aim to convert the C#-like code from the book into Elixir, so I can make presentations to people and help them both understand the basic concepts of DDD while making them familiar with Elixir.

Code

I am tackling chapter 5, specifically the Transaction Script. In this hypothetical scenario, we have a database that must log user visits. The Entity Diagram for this imaginary database is as follows:

The original code by the author is the following:

public class LogVisit
{
  // ... setup code ...
  
  public void Execute(Guid userId, DateTime visitedOn){
    _db.Execute("UPDATE Users SET last_visit=@p1 WHERE user_id=@p2", visitedOn, userId);

    _db.Execute(@"INSERT INTO VisitsLog(user_id, visit_date) VALUES(@p1, @p2)", userId, visitedOn);
  }
}

Elixir Conversion

The direct translation of that code into Elixir would be the following (or close to it):

  @spec execute_v1(integer(), String.t()) :: Ecto.Adapters.SQL.query_result()
  def execute_v1(user_id, visited_on) do
    Repo.query!("UPDATE \"user\" SET last_visit=\'#{visited_on}\' WHERE id=#{user_id}")

    Repo.query!(
      "INSERT INTO \"visit\" (user_id, visit_date) VALUES (#{user_id}, \'#{visited_on}\');"
    )
  end

I feel this is equal enough to the original, if you understand the original, I believe you will also understand this one.

The issue now comes when I try to improve upon it. My aim here is to make this code idiomatic to elixir. This means using Ecto and many other Elixir constructs.

My first iteration is as follows:

@spec execute_v2(integer(), DateTime.t()) :: :ok | {:error, any()}
  def execute_v2(user_id, visited_on) do
    with {:ok, user} <- get_user(user_id),
         :ok <- update_user(user, visited_on) do
      insert_new_visit(user_id, visited_on)
    end
  end

  @spec get_user(integer) :: {:ok, Schema.t()} | {:error, :user_not_found}
  defp get_user(user_id) do
    case Repo.get(User, user_id) do
      nil -> {:error, :user_not_found}
      %User{} = user -> {:ok, user}
    end
  end

  @spec update_user(Schema.t(), DateTime.t()) :: :ok | {:error, Changeset.t()}
  defp update_user(user, visited_on) do
    user
    |> User.changeset(%{last_visit: visited_on})
    |> Repo.update()
    |> case do
      {:ok, _user} -> :ok
      error -> error
    end
  end

  @spec insert_new_visit(integer(), DateTime.t()) :: :ok | {:error, Changeset.t()}
  defp insert_new_visit(user_id, visited_on) do
    %Visit{user_id: user_id, visit_date: visited_on}
    |> Repo.insert()
    |> case do
      {:ok, _visit} -> :ok
      error -> error
    end
  end

If you have never seen Elixir before, I fear this may be too much. I have 3 functions instead of 1, I am using constructs that many people won’t recognize (with) and I am not sure that using the pipe operator |> and the case expressions is obvious enough.

On top of that I am also using Ecto.Changeset, which is a discussion on its own.

However, I feel like I need Ecto here, because this example is then expanded on in order to add transactions with a classical try-catch:

public class LogVisit
{
  // ... setup code ...
  
  public void Execute(Guid userId, DateTime visitedOn){
    try{
      _db.StartTransaction();

      _db.Execute("UPDATE Users SET last_visit=@p1 WHERE user_id=@p2", visitedOn, userId);
      _db.Execute(@"INSERT INTO VisitsLog(user_id, visit_date) VALUES(@p1, @p2)", userId, visitedOn);

      _db.Commit();
    } catch {
      _db.Rollback();
      throw;
    }
  }
}

Questions

  • As someone new to the technology, would this be easy for you to understand?
  • Is there a way to make this example more idiomatic to Elixir, without increasing the complexity too much?
  • What is the simplest code you could write that would mimic both the naive version and the transaction version?

Marked As Solved

al2o3cr

al2o3cr

IMO a better translation would also use parameters, to avoid the obvious SQL-injection vulnerability:

  @spec execute_v1(integer(), String.t()) :: Ecto.Adapters.SQL.query_result()
  def execute_v1(user_id, visited_on) do
    Repo.query!("UPDATE \"user\" SET last_visit=$1 WHERE id=$2", [visited_on, user_id])

    Repo.query!(
      "INSERT INTO \"visit\" (user_id, visit_date) VALUES ($1, $2);",
      [visited_on, user_id])
    )
  end

The final version with a transaction can use Repo.transaction and rely on query! raising:

  @spec execute_v2(integer(), String.t()) :: {:ok, :whatever}
  def execute_v2(user_id, visited_on) do
    Repo.transact(fn ->
      Repo.query!("UPDATE \"user\" SET last_visit=$1 WHERE id=$2", [visited_on, user_id])

      Repo.query!(
        "INSERT INTO \"visit\" (user_id, visit_date) VALUES ($1, $2);",
        [visited_on, user_id])
      )

      {:ok, :whatever}
    end)
  end

Also Liked

LostKobrakai

LostKobrakai

There’s nothing bad about it. It’s just very verbose for something that in most cases could be solved without all the verboseness and closer to plain elixir.

LostKobrakai

LostKobrakai

Having worked extensively with Multi I’d strongly suggest skipping it. It’s most useful where parts of a query are composed by user input I’d argue. Elsewhere you’re better to skip it.

@spec execute(integer(), DateTime.t()) :: :ok
def execute(user_id, visited_on) do
  Repo.transaction(fn -> 
    User
    |> where(id: ^user_id)
    |> Repo.update_all([set: [last_visited: visited_on]]
    
    %Visit{user_id: user_id, visit_date: visited_on}
    |> Repo.insert!()
  end)

  :ok
end

Personally I’d do a lot less converting the code example to elixir/ecto. This retains the same interface and functionality as the code example while translating what is there to ecto. Any additional code goes beyond what had been there and I guess one could argue for Repo.insert_all and "users" as the table for the update to not need a schema, but I’d consider that too far from ideomatic.

dimitarvp

dimitarvp

Thanks for the summary, I appreciate it.

Yes it’s a bit verbose. I’m simply looking at it as an Enum.reduce accumulator of sorts.

krasenyp

krasenyp

I’ve used Ecto.Multi extensively and I like it. Especially when I need to do a bulk operation but I can’t use insert_all or update_all. I find Repo.transact as ergonomic as Multi though, just for different situations.

seeplusplus

seeplusplus

  • As someone new to the technology, would this be easy for you to understand?
  • Is there a way to make this example more idiomatic to Elixir, without increasing the complexity too much?

Hopefully this isn’t too ranty, but I wanted to share my thoughts. Two things:

  • 3 functions aren’t necessarily more complicated than 1. I wouldn’t worry about this.
  • Using language or framework specific grammar/syntax isn’t more complex* than keeping things “familiar”.

With the availability of LLMs, the second point is especially true. The more you can leverage well-documented and widely known grammar/syntax, the more clear it will be. Regardless of whether the reader has been exposed them a priori. This is especially true in the age of AI, where a cogent explanation is only a chat prompt away. I understand the apprehension that “the reader may not know Elixir,” but these language features are designed to be intuitive and I think the industry has a tendency to over-estimate on how difficult it will be for someone to understand, for example, the pipe operator.

Edit: Also fwiw I showed your execute_v2 code to an intern with no Elixir experience and they said it was easy to read and they were able to describe what it does with good accuracy. I know this is a single data point, but my point here is that the average dev is pretty good at guessing what well-design syntax does.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
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
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New

Other popular topics Top

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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement