This episode is for subscribers only. Sign Up or Log in to watch it.

26. Component and System

Published 07 March 17

Stuart Sierra’s Component is a small library that can have a big impact on the structure and architecture of your application. In this episode you learn how you can use it to create components, and manage dependencies. You will create a Jetty and an Endpoint component, and use the PostgreSQL component from the System project.

Show notes

Component calls itself a tiny framework for managing the lifecycle and dependencies of software components which have runtime state. It’s a helpful tool for making runtime dependencies explicit, and it’s a great addition to a REPL based workflow.

Component is platform independent, the examples in this episode will focus on Clojure, but you can use it in ClojureScript just the same.

Component makes heavy use of records and protocols, two Clojure features that not everyone will be familiar with. If you need a refresher you can check out episode 23 and 24, which cover these in depth.

With that out of the way, let’s have a look at Component. Component is just a library, so to get started add it as a dependency to your project.clj or build.boot.

(defproject component-example "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.8.0"]
                 [com.stuartsierra/component "0.3.2"]])

If you want to follow along you can also clone the lambdaisland/component_example project from Github and grab the ep26-start branch. This branch already contains all dependencies used in this episode, as well as a few utility functions that will come in handy later on.

git clone https://github.com/lambdaisland/component_example.git
cd component_example
git checkout ep26-start

browse source code

The starting point for this episode is the ep26-start branch, the final result is in ep26-end. (diff)

Libraries

Discussions