clj-di.core
Functions and macros for using dependency injection.
defprotocol*
macro
(defprotocol* name & body)
Defines protocol and creates proxy methods where protocol implementation received as a dependency.
For defining dependency you should:
(defprotocol* http-client
(get [_ url request])
(post [_ url request]))
After that you have http-client
protocol and get*
and post*
proxy functions.
Then you need to implement protocol:
(deftype HttpClient
[]
http-client
(get [_ url request] (http/get url request))
(post [_ url request] (http/post url request)))
And register dependency:
(register! :http-client (HttpClient.))
And after that you can use proxy functions, like:
(get* "http://nvbn.github.io/" {})
(post* "http://nvbn.github.io/" {:transit-params {:a 1 :b 2}})
get-dep
(get-dep key)
Get dependency by name.
Usage:
(get-dep :http)
Dependency should be registered with register! or with-registered.
let-deps
macro
(let-deps deps & body)
Bind dependencies in lexical scope of code block.
Usage:
(let-deps [http :http
logger :logger]
...)
It will work like:
(let [http (get-dep :http)
logger (get-dep :logger)]
...)
Dependencies should be registered with register! or with-registered.
register!
(register! & key-dep-pairs)