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?























