Hi,

I am trying to wrap my head around how to best use the Component library 
for structuring my app.

Let's say I have a simple Store protocol:

(defprotocol Store
  (get [store k])
  (put [store k v]))

The production Store protocol implementation would, as an example, be 
backed by Riak but in my tests I would like to mock it out with an 
in-memory version. What would be the best way of doing so?

Option 1, separate lifecycle from the actual store:

(deftype InMemoryStore [backing-map]
    Store
    (get [_ k]
        (@backing-map k))
    (put [_ k v]
        (swap! backing-map assoc k v)))

(defn create-memory-store
    []
    (InMemoryStore. (atom {})))        

;; create-fn must return something that implements the Store protocol       
  
(defrecord StoreComponent [store create-fn]
    component/Lifecycle
    
    (start [component]
        (assoc component :store (create-fn))
            
    (stop [component]
        (assoc component :store nil)))
        
(defn new-store [create-fn]
    (map->StoreComponent {:create-fn create-fn}))

Option 2, create different components

(defrecord InMemoryStore [backing-map]
    component/Lifecycle
    
    (start [component]
        (assoc component :backing-map (atom {}))
            
    (stop [component]
        (assoc component :backing-map nil))
        
    Store
    (get [_ k]
        (@backing-map k))
    (put [_ k v]
        (swap! backing-map assoc k v)))
        
(defn new-memory-store []
    (map->InMemoryStore {}))

And then inject the correct component in the system map depending on 
settings.

Option 3, something  completely different?

Both options seem quite cumbersome when all I want to be able to do in my 
tests is to 'assoc' a map instead of a production Store implementation. Am 
I just misunderstanding how to use the Component library?

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to