Liking cljdoc? Tell your friends :D

Dependencies

(ns darkleaf.di.tutorial.b-dependencies-test
  (:require
   [clojure.test :as t]
   [darkleaf.di.core :as di])
  (:import
   (clojure.lang ExceptionInfo)))

DI uses associative destructuring syntax to define dependencies of a component. https://clojure.org/guides/destructuring#_associative_destructuring

There is a mapping between keys and components. A key can be symbol, keyword, or string. In this chapter we'll use only symbols.

If we use symbols DI will try to resolve a component's var.

In the following example the root component depends on the a and b and the b is optional. You also can get all component dependencies by the deps binding.

(defn root
  {::di/kind :component}
  [{a      `a
    ::syms [b]
    :or    {b ::default}
    :as    deps}]
  [:root a b deps])

(def a ::a)

(t/deftest root-test
  (with-open [root (di/start `root)]
    (t/is (= [:root ::a ::default {`a ::a}] @root))))

di/start can accept additional arguments. In the following example the argument is a map registry. I use it to define local keys. In general they are middlewares but I'll describe it later.

(t/deftest root-with-extra-deps-test
  (with-open [root (di/start `root {`b ::b})]
    (t/is (= [:root ::a ::b {`a ::a `b ::b}] @root))))

Dependencies are required by default. There is no definition of a' so DI will throw an exception.

(defn root'
  {::di/kind :component}
  [{a `a'}]
  [::root a])

(t/deftest root'-test
  (t/is (thrown-with-msg? ExceptionInfo
                          #"Missing dependency darkleaf.di.tutorial.b-dependencies-test/a'"
                          (di/start `root'))))

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close