I was surprised when i found that no one wrote library for dependency injection which works on clojure and clojurescript. I found component, but it’s too complex and not working with clojurescript.
So i developed little library – clj-di. I wrote it using cljx, so library can work with clojure and clojurescript. Library uses atom with hash map for storing dependencies. Dependency can be registered and received using keyword.
Library has two ways for registering dependencies. Permanently with
(i use it in
:init for creating db connections):
(register! :db (Database.) :logger (get-logger))
And for code block with
with-registered (i use it in tests for registering mocks):
(with-registered [:db (Database.) :logger (get-logger)] ...)
And two ways for receiving dependency. With
get-dep function (useful for receiving single dependency):
let-deps macro (useful for receiving more than one dependency):
(let-deps [conn :db log :logger] ..)