Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8925863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:48:50+00:00 2026-06-15T07:48:50+00:00

Still learning clojure and doing an experiment on trying to wrap my head around

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:48:56+00:00Added an answer on June 15, 2026 at 7:48 am

    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” item in 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´m still learning my way around MVC programming and have one question for you
Newbie still learning, been trying to search and hack code together for hours now,
I am still learning about SIP and all its protocols, specifically trying to integrate
Still learning magento coding. I wonder is there a way I can print out
Still learning WPF....thanks for any help. Is there any way to Refactor this: <ListBox
Still learning JSP Web Applications here. I have been doing this for a while
I'm still learning js and trying out the Webstorm IDE, which seems sweet (including
Hi sorry still learning here and slow to learning code arguments. Just wondering could
Still learning the MySQL ropes and I'm trying to find out how to do
Still learning my ways around C# generics... is it possible to achieve this functionality?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.