liveBook · Manning

Separation of concerns

As explained in Elixir in Action, Third Edition

Separation of concerns is a design technique that divides a system into components with distinct responsibilities.

Separate distinct responsibilities into independent abstractions, then compose them. A MultiDict owns multiple values per key, while TodoList adds behavior specific to to-do lists. section 4.1.2

Keep HTTP at the boundary

Parse request parameters and format responses in the web layer, but delegate operations to HTTP-agnostic domain code such as Todo.Server. This keeps the core easier to work with, test, and expand. section 11.3.3

Listing 4.4 Implementing the MultiDict abstraction (todo_multi_dict.ex)
defmodule MultiDict do
 def new(), do: %{}

 def add(dict, key, value) do
   Map.update(dict, key, [value], &[value | &1])
 end

 def get(dict, key) do
   Map.get(dict, key, [])
 end
end

Inspect the MultiDict implementation that isolates keyed collection behavior from TodoList responsibilities. section 4.1.2

How Elixir in Action, Third Edition covers it

How deep it goes
Demonstrates the technique through Elixir modules, composition, request handling, and practical benefits for testing and expansion.
Examples
Uses MultiDict and TodoList abstractions plus add_entry and entries HTTP requests backed by Todo.Server.
What it assumes
Assumes familiarity with Elixir modules, functions, maps, and HTTP request handling.
What you can do after
Apply it via Elixir modules and Plug handlers to isolate reusable data behavior, domain logic, and HTTP concerns.
How it connects
Connects data abstractions, modules, functions, HTTP interfaces, domain-specific types, testing, and composition.

Also explained in

Compare how 22 books explain separation of concerns →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.