Liking cljdoc? Tell your friends :D

Clojars Project cljdoc badge

Dependency injection

DI is a dependency injection framework that allows you to define dependencies as easily as you define function arguments.

It uses plain clojure functions and associative destructuring to define a graph of functions and stateful objects.

(ns app.core
  (:require
   [darkleaf.di.core :as di]
   [ring.adapter.jetty :as jetty]
   [app.adapters.reitit :as-alias reitit]
   [app.adapters.hikari :as-alias hikari]
   [app.adapters.db     :as-alias db]))

(defn show-user [{ds ::db/datasource} req]
  ...)

(def route-data
  (di/template
    [["/users/:id" {:get {:handler (di/ref `show-user)}}]]))

(defn jetty
  {::di/stop (memfn stop)}
  [{handler ::handler
    port    :env.long/PORT
    :or     {port 8080}}]
  (jetty/run-jetty handler {:join? false, :port port}))

(di/start `jetty
          (di/env-parsing :env.long parse-long)
          {::handler           (di/ref `reitit/handler)
           ::reitit/route-data (di/ref `reitit/data)
           ::db/datasource     (di/ref `hikari/datasource)
           "PORT"              "9090"})

It is just a short snippet, please see example app.

Install

{:deps {org.clojars.darkleaf/di {:mvn/version "3.6.3"}}}
;; or
{:deps {org.clojars.darkleaf/di {:git/url "https://github.com/darkleaf/di.git"
                                 :sha     "%SHA%"}}}

Documentation

Full documentation, tutorials, and API reference are available on cljdoc.

See also the example app, starting with user.clj.

Versions

  • See 1.0 branch for previous version
  • See master branch for current version

License

Copyright © 2022 Mikhail Kuzmin

Licensed under Eclipse Public License v2.0 (see LICENSE).

Can you improve this documentation? These fine people already did:
Mikhail Kuzmin & darkleaf
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