Quantcast
Channel: Repository pattern in clojure - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Repository pattern in clojure

$
0
0

Being new to clojure, I would like some advice on implementing the repository pattern*.

In an OO language, I would create a repository interface, a test and at least one db impl. I would instantiate them during bootstrap, pass the objects around using DI or get them through a service locator. I'm guessing it's done completely different in clojure?

1) What is a good way of grouping the functions in the repo? Protocol, "free" functions in a namespace?

2) Where do I instantiate the repository back-end, i.e. allocate resources like db-connections etc.? Do I instantiate an implementation of the repository protocol and assign it to an atom, or in the case of free functions, redefine them?

*) A repository is a back-end abstraction for persistence and typically supports a CRUD style range of operations.

EDIT: Here is the approach I'm currently using. A protocol to group functions. A test and a "real" record implementing it. Then an atom to register the repo with.

(defprotocol GardenRepo"Repository of Gardens. Supports CRUD style operations."  (list-gardens [repo] "Get a seq of all gardens in the repo.")  (get-garden [repo id] "Get a specific garden by it's id.")  ...)                                                                (let [repo (atom nil)]  (defn get-garden-repo [] @locator)  (defn set-garden-repo [new-repo] (reset! repo new-repo)))

Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>