@@ -471,6 +471,65 @@ on whether the application uses the `:main` or `:repl` profile.
471
471
:name #ig/var name}}}
472
472
----
473
473
474
+ === Integrant
475
+
476
+ So far we've used functions to implement components. The
477
+ `:tutorial.print.hello` component was defined by:
478
+
479
+ .src/tutorial/print.clj
480
+ [,clojure]
481
+ ----
482
+ (ns tutorial.print
483
+ (:require [duct.logger :as log]))
484
+
485
+ (defn hello [{:keys [level logger name]}]
486
+ (log/log logger level ::hello {:name name}))
487
+ ----
488
+
489
+ But this is just convenient syntax sugar for Integrant's `init-key`
490
+ method. The following code is equivalent to the previous component
491
+ definition:
492
+
493
+ .src/tutorial/print.clj
494
+ [,clojure]
495
+ ----
496
+ (ns tutorial.print
497
+ (:require [duct.logger :as log]
498
+ [integrant.core :as ig))
499
+
500
+ (defmethod ig/init-key ::hello [_key {:keys [level logger name]}]
501
+ (log/log logger level ::hello {:name name}))
502
+ ----
503
+
504
+ Duct uses Integrant for its component definitions, and Integrant
505
+ provides several multimethods to this end. The most common one is
506
+ `init-key`. If no such method is found, Integrant searches for a
507
+ function of the same name.
508
+
509
+ There is also `halt-key!`, which defines a teardown procedure for a key.
510
+ This can be useful for cleaning up files, threads or connections that
511
+ the `init-key` method (or function) opened. The return value from
512
+ `init-key` will be passed to `halt-key!`.
513
+
514
+ .src/tutorial/print.clj
515
+ [,clojure]
516
+ ----
517
+ (ns tutorial.print
518
+ (:require [duct.logger :as log]
519
+ [integrant.core :as ig))
520
+
521
+ (defmethod ig/init-key ::hello [_key {:keys [level logger name] :as opts}]
522
+ (log/log logger level ::hello {:name name})
523
+ opts)
524
+
525
+ (defmethod ig/halt-key! ::hello [_key {:keys [level logger name]}]
526
+ (log/log logger level ::goodbye {:name name}))
527
+ ----
528
+
529
+ For more information on the multimethods that can be used, refer to the
530
+ https://github.com/weavejester/integrant/blob/master/README.md[Integrant
531
+ documentation].
532
+
474
533
== Web Development
475
534
476
535
While Duct can be used for any server-side application, its most common
0 commit comments