Managing Stateful Components

Web Development with Clojure, Third Edition — by Dmitri Sotnikov, Scot Brown (23 / 107)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Think in Terms of Application Components | TOC | What You’ve L earned 👉

Most applications rely on stateful external resources, such as database connections and queues. These resources often have a life cycle associated with them. Typically, we’d like to start such resources when our application initializes and stop them when it shuts down.

To facilitate a REPL-driven workflow, Luminus uses the Mount library to manage the life cycle of stateful resources in the application. This library treats resources as variables bound to namespaces — an approach that allows Mount to automatically infer the order that the components need to be started from the namespace hierarchy. Naturally, this means that we should be careful with how we access these variables.

Conceptually, Mount takes the approach of encapsulating stateful resources using namespaces. This leads to a natural separation between the code that deals with state from the pure core of the application business logic.

It helps to treat the core business logic as you would a library. It should be completely independent from where the data is coming from and where it’s going. As a rule, we should avoid passing resources directly to our business logic. Instead, we should create a thin layer that deals with external resources and calls the business logic to process the…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.