(defn a-fun [{:keys [db-port db-url]}]
{:db-connection some-conn})
For this function, it is known that given db-port
and db-url
it will given back a db-connection
. We can use clojure macro to parse these information
(require '[com.github.clojure.di.core :refer [defdi execute]])
(defdi http-server [{:keys [http-port route]
db-conn :db-connection}]
;start http server
{:http-server xxx})
(defdi database [{:db/keys [url port user password]}]
; create db connection
{:db-connection the-connection})
;; other di
(defdi ...)
;;then run the di by
(execute [http-server database ...])
defdi
has the same syntax as defn
, and returns a normal function just as defn
, except
- It must have exactly one argument
- It must return a map with keyword as keys
Then by run (execute [http-server database ...])
, it will calculate the dependency graph, and excute the di functions one by one with their dependency order
For convenience, this library also provide a bootstrap
function, to help you bootstrap you application
(require '[com.github.clojure.di.app :refer [bootstrap]])
(bootstrap "com.example")
Then, the libary will scan all the namespace in the classpath which has the prefix com.example
, and collect all the di component defined by defdi
, and excute them.
Thus, you don't need to care about where to import the depenedency, just let the library to handle it
For more detail usage, refer the tests