Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Component, What and Why

Component: What and Why

Problem:

At some point when you’re building a clojure app or a service, you realize that you’ve got a lot of things that refer to external dependencies. These can be database connections, loggers, metrics publishers, etc.

When I started building apps I’d just pass these dependencies into routes or go-loops or other semi-stateful parts of my system and call it a day.

It works ok, but:

  • It’s hard to test.
  • You don’t know what depends on what.
  • You can’t turn things on and off very easily.

Solution:

Component tries to take this state and turn it into a series of components. A component is really just a record with a pretty light protocol on it that lets you start and stop it.

Components let you do the following:

  • Define state that you can pass around your system.
  • Explicitly define your dependencies (which you need if you want to stop, start or reload pieces)
  • Mock out pieces of your system (It’s a lot easier to test stateful pieces if you just mock them out with a core.async channel that you can check at test-time).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment