Re: Please share your thoughts on dependency injection

2010-02-09 Thread Steve Purcell
On 8 Feb 2010, at 16:53, Boris Mizhen - 迷阵 wrote:

> 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.


I think you could achieve the same effect much more simply using 
'binding'/'with-bindings' and 'memoize'.

-Steve

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Please share your thoughts on dependency injection

2010-02-08 Thread Boris Mizhen - 迷阵
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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en