Book Review: Programming Phoenix

Phoenix is basically the official web framework for Elixir now (As José Valim, the Elixir creator, is also the core maintainer of Phoenix). And I'm really excited by the ideas Elixir/Phoenix brought to the web development community. I'll explain some of them in this blog post.

Caveats

Programming Phoenix 1.3 is NOT released yet. (Not until Feb/Mar 2018)

So I also spent some time to learn about Context (which is a breaking change in Phoenix 1.3).

Functional MVC

A web server is a natural problem for a functional language to solve.

We can think of any web server as a huge function collections:

Function Name
URL
Function Input
Request Payload
Function Output
Response

The whole job of our web server is to provide these different functions and deal with user input.

This makes functional programming languages (like Elixir) a perfect fit for web development.

And a whole trip of a request in a Phoenix application can be expressed in Elixir's data pipeline like this:

connection
|> endpoint
|> router # directs a request into the appropriate controller
|> pipelines # groups functions together to handle common tasks
|> controller

This data flow is super neat because we can then break down each steps (like endpoint, router) even further into different pipelines (which is the basic abstraction model in Elixir). And this is the idea behind Phoenix (or Plug, the connection adapter behind Phoenix)

Ecto: Your Functional Data Source

Ecto.Repo is not using Repository Pattern

Ecto.Repo is not a Repository1 (defined by P of EEA), but more like a Data Mapper2.

You can see the discussion here3.

More Explicit

Compared to ActiveRecord4, Ecto (or even Phoenix and Elixir) values explicity higher than implicity.

Things like associations It's been talked many times in the community. So I'll skip this part.

SQL Sandbox is awesome

Being explicit gives us more control over the db connections.

Ecto.Adapters.SQL.Sandbox.checkout guarantees that connection runs in a transaction and automatically rolls back the db at the end of every test.

So we can run our system tests concurrently, which is super hard in the Ruby/Rails world.

Ecto 2.1

If you want to know more about Ecto, What's New in Ecto 2.15 is a book you cannot miss.

Summary

I'm really excited to learn Phoenix and the functional way to build a web application. I'll definitely try to build an app in Phoenix in the near future.