(add-side-dependency dep-key)
A registry middleware for adding side dependencies. Use it for migrations or other side effects.
(defn flyway [{url "DATABASE_URL"}]
(.. (Flyway/configure)
...))
(di/start ::root (di/add-side-dependency `flyway))
A registry middleware for adding side dependencies. Use it for migrations or other side effects. ```clojure (defn flyway [{url "DATABASE_URL"}] (.. (Flyway/configure) ...)) (di/start ::root (di/add-side-dependency `flyway)) ```
(combine-dependencies)
(combine-dependencies a b)
Combines dependencies. Use it with reduce
.
Dependencies are a hash map of a key and a dependency type.
Combines dependencies. Use it with `reduce`. Dependencies are a hash map of a key and a dependency type.
(env-parsing & {:as cmap})
A registry middleware for parsing environment variables. You can define a dependency of env as a string key like "PORT", and its value will be a string. With this middleware, you can define it as a qualified keyword like :env.long/PORT, and its value will be a number. cmap is a map of prefixes and parsers.
(defn root [{port :env.long/PORT}]
...)
(di/start `root (di/env-parsing :env.long parse-long
:env.edn edn/read-string
:env.json json/read-value))
A registry middleware for parsing environment variables. You can define a dependency of env as a string key like "PORT", and its value will be a string. With this middleware, you can define it as a qualified keyword like :env.long/PORT, and its value will be a number. cmap is a map of prefixes and parsers. ```clojure (defn root [{port :env.long/PORT}] ...) (di/start `root (di/env-parsing :env.long parse-long :env.edn edn/read-string :env.json json/read-value)) ```
(fmap factory f & args)
Applies f to an object that the factory produces. f accepts a built object and returns updated one.
f should not return a non-trivial instance of p/Stoppable
.
(def port (-> (di/ref "PORT")
(di/fmap parse-long)))
See ref
, template
.
Applies f to an object that the factory produces. f accepts a built object and returns updated one. f should not return a non-trivial instance of `p/Stoppable`. ```clojure (def port (-> (di/ref "PORT") (di/fmap parse-long))) ``` See `ref`, `template`.
(instrument f & args)
A registry middleware for instrumenting or decorating built objects. Use it for logging, schema checking, AOP, etc.
f and args are keys.
Also f can be a function in term of ifn?
.
A resolved f must be a function of [object key & args] -> new-object
.
f should not return a non-trivial instance of p/Stoppable
.
It is smart enough not to instrument f's dependencies with the same f to avoid circular dependencies.
(defn stateful-instrumentaion [{state :some/state} key object arg1 arg2] ...)
(di/start ::root (di/instrument `stateful-instrumentation `arg1 ::arg2 "arg3")))
(defn stateless-instrumentaion [key object arg1 arg2 arg3] ...)
(di/start ::root (di/instrument stateless-instrumentation `arg1 ::arg2 "arg3"))
(di/start ::root (di/instrument #'stateless-instrumentation `arg1 ::arg2 "arg3"))
See start
, update-key
, fmap
.
A registry middleware for instrumenting or decorating built objects. Use it for logging, schema checking, AOP, etc. f and args are keys. Also f can be a function in term of `ifn?`. A resolved f must be a function of `[object key & args] -> new-object`. f should not return a non-trivial instance of `p/Stoppable`. It is smart enough not to instrument f's dependencies with the same f to avoid circular dependencies. ```clojure (defn stateful-instrumentaion [{state :some/state} key object arg1 arg2] ...) (di/start ::root (di/instrument `stateful-instrumentation `arg1 ::arg2 "arg3"))) (defn stateless-instrumentaion [key object arg1 arg2 arg3] ...) (di/start ::root (di/instrument stateless-instrumentation `arg1 ::arg2 "arg3")) (di/start ::root (di/instrument #'stateless-instrumentation `arg1 ::arg2 "arg3")) ``` See `start`, `update-key`, `fmap`.
(opt-ref key)
Returns a factory referencing to a possible undefined key. Produces nil in that case.
See template
, ref
, fmap
.
Returns a factory referencing to a possible undefined key. Produces nil in that case. See `template`, `ref`, `fmap`.
(ref key)
Returns a factory referencing to a key.
(def port (di/ref "PORT"))
(defn server [{port `port}] ...)
(def routes (di/template [["/posts" (di/ref `handler)]]))
(di/start `root {::my-abstraction (di/ref `my-implementation)})
See template
, opt-ref
, fmap
, p/build
.
Returns a factory referencing to a key. ```clojure (def port (di/ref "PORT")) (defn server [{port `port}] ...) (def routes (di/template [["/posts" (di/ref `handler)]])) (di/start `root {::my-abstraction (di/ref `my-implementation)}) ``` See `template`, `opt-ref`, `fmap`, `p/build`.
(start key & middlewares)
Starts a system of dependent objects.
key is a name of the system root. Use symbols for var names, keywords for abstract dependencies, and strings for environments variables.
key is looked up in a registry. By default registry uses Clojure namespaces and system env to resolve symbols and strings, respectively.
You can extend it with registry middlewares. Each middleware can be one of the following form:
registry -> key -> Factory
p/Factory
instanceMiddlewares also allows you to instrument built objects.
It's useful for logging, schema validation, AOP, etc.
See instrument
, update-key
.
(di/start `root
{:my-abstraction implemntation
`some-key replacement
"LOG_LEVEL" "info"}
[dev-middlwares test-middlewares]
(if dev-routes?
(di/update-key `route-data conj `dev-route-data)
(di/instrument `log))
Returns a container contains started root of the system.
The container implements AutoCloseable
, Stoppable
, IDeref
, IFn
and Indexed
.
Use with-open
in tests to stop the system reliably.
You can pass a vector as the key argument to start many keys:
(with-open [root (di/start [`handler `helper])]
(let [[handler helper] root]
...))
See the tests for use cases.
See instrument
, update-key
.
Starts a system of dependent objects. key is a name of the system root. Use symbols for var names, keywords for abstract dependencies, and strings for environments variables. key is looked up in a registry. By default registry uses Clojure namespaces and system env to resolve symbols and strings, respectively. You can extend it with registry middlewares. Each middleware can be one of the following form: - a function `registry -> key -> Factory` - a map of key and `p/Factory` instance - nil, as no-op middleware - a sequence of the previous forms Middlewares also allows you to instrument built objects. It's useful for logging, schema validation, AOP, etc. See `instrument`, `update-key`. ```clojure (di/start `root {:my-abstraction implemntation `some-key replacement "LOG_LEVEL" "info"} [dev-middlwares test-middlewares] (if dev-routes? (di/update-key `route-data conj `dev-route-data) (di/instrument `log)) ``` Returns a container contains started root of the system. The container implements `AutoCloseable`, `Stoppable`, `IDeref`, `IFn` and `Indexed`. Use `with-open` in tests to stop the system reliably. You can pass a vector as the key argument to start many keys: ```clojure (with-open [root (di/start [`handler `helper])] (let [[handler helper] root] ...)) ``` See the tests for use cases. See `instrument`, `update-key`.
(template form)
Returns a factory for templating a data-structure.
Replaces ref
or opt-ref
instances with built objects.
(def routes (di/template [["/posts" (di/ref `handler)]]))
See ref
and opt-ref
.
Returns a factory for templating a data-structure. Replaces `ref` or `opt-ref` instances with built objects. ```clojure (def routes (di/template [["/posts" (di/ref `handler)]])) ``` See `ref` and `opt-ref`.
(update-key target f & args)
A registry middleware for updating built objects.
target is a key to update.
f and args are keys.
Also f can be a function in term of ifn?
.
f should not return a non-trivial instance of p/Stoppable
.
(def routes [])
(def subsystem-routes (di/template [["/posts" (di/ref `handler)]]))
(di/start ::root (di/update-key `routes conj `subsystem-routes))
If you don't want to resolve keys like :some-name, you should use them in a in-place fn:
(di/update-key `key #(assoc %1 :some-name %2) `some-value)
See update
, start
, instrument
, fmap
.
A registry middleware for updating built objects. target is a key to update. f and args are keys. Also f can be a function in term of `ifn?`. f should not return a non-trivial instance of `p/Stoppable`. ```clojure (def routes []) (def subsystem-routes (di/template [["/posts" (di/ref `handler)]])) (di/start ::root (di/update-key `routes conj `subsystem-routes)) ``` If you don't want to resolve keys like :some-name, you should use them in a in-place fn: ```clojure (di/update-key `key #(assoc %1 :some-name %2) `some-value) ``` See `update`, `start`, `instrument`, `fmap`.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close