Still learning clojure and doing an experiment on trying to wrap my head around clojure and protocols. My main question is my code the correct way to extract away the low-level database interactions in order to provide a cleaner way of adding specific datatypes to my database?
(defn- db-insert
[table record]
"Inserts record based on table/record map"
(sql/with-connection
rosay-db
(sql/transaction
(sql/insert-record table record))))
;; public interface
(defprotocol DBFactory
(add-item [_] "Adds item to db"))
(defrecord Page [name description keywords frontpage client_id pages_type_id]
DBFactory
(add-item [_]
(db-insert :pages {:name name
:description description
:keywords keywords
:frontpage frontpage
:client_id client_id
:pages_type_id pages_type_id})))
(defrecord Client [username password email domain]
DBFactory
(add-item [_]
(db-insert :clients {:username username
:password password
:email email
:domain domain})))
Then in my nrepl I can do the following:
rosay.server=> (use 'rosay.models)
nil
rosay.server=> (add-item (->Client "booyaka" "fark" "mailzer" "domain.com"))
{:updated_on nil, :created_on #inst "2012-12-04T04:25:22.672462000-00:00", :email "mailzer", :domain "domain.com", :password "fark", :username "booyaka", :id 10}
I guess im just curious if my way of attacking this problem is correct or even makes sense. So far I can do both Clients and Pages by simply calling my protocol with my record type and everything seems to work. Perhaps I’m overthinking the problem and just writing a few functions is all?
Thanks
IMO it’s not very useful to hide difference between two distant types Client and Page only in db interaction. It doesn’t reduce/abstract code without other polymorphic operations (e.g., object creation, object processing, etc). Anyway, these types is too different to “be the same”
itemin all app. It’s more clear just create separate operations for these types (eg, add-page, add-client). If you really need this you can compare your solution to solution with multimethods which provides dispatch mechanism but don’t force you to use OOP.