You can use a higher order function to represent your DAO level - this is the essence of functional programming, using functions to represent small and large parts of your system. Thus, you have a higher-order function that takes a connection to the database as a parameter and returns another function that you can use to call various operations, such as saving, deleting, etc. In the database. The following is one such example:
(defn db-layer [db-connection] (let [db-operations {:save (fn [obj] (save db-connection obj)) :delete (fn [obj] (delete db-connection obj)) :query (fn [query] (query db-connection query))}] (fn [operation & params] (-> (db-operations operation) (apply params)))))
Using DB level:
(let [my-db (create-database) db-layer-fn (db-layer my-db)] (db-layer-fn :save "abc") (db-layer-fn :delete "abc"))
This is just an example of how higher order functions can allow you to create a context for another set of functions. You can take this concept even further by combining it with other Clojure features, such as protocols.
Ankur source share