Hello all,
I am playing with the idea of a little library for dependency injection.
The idea is to declare injectable values as metadata-to-function map.
I started with a sketch of what the client code may look like.
Please let me know what you think.
Thank you,
Boris
Dependency declaration code:
;; -------------------------------------
;; declare lookup functions
(def-inject
#^{:type String :name "password"} password-lookup-fn
#^{:a 1 :b 2} a1-b2-lookup-fn
....)
;; utility wrapper functions that allow for caching, lazy
initializations etc ...
(def-inject
#^{:type JDBC :kind "mysql"} (lazy (once-per-thread mysql-connect))
#^{:type JDBC :kind "oracle"} (lazy (once-per-thread oracle-connect)))
Client code:
;;-------------------------------------------------
;; a function that has access to password, foo and bar in it's lexical scope
;; dependencies are bound when the function is called
(inject
#^{:name "password" } password
#^{:a 1 :b 2} foo
#^{:type t2 :p1 v1 :p2 v2} bar
(defn fn-with-foo-bar-injected..... ))
;; overriding injected values - e.g. for unit testing
(inject-bind
foo foo-val
bar bar-val
(fn-with-foo-bar-injected ..... ))
;; injecting a value inline
;; the name foo is provided for inject-bind
(defn ..... (... (inject #^{:type t :param1 v1 :param2 v2} foo) ...) ...)
;; local scope injections - works like let
(inject #^{:type t :param1 v1 :param2 v2} foo (...code that can use foo...))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en