Multimethod or protocol or...? Clojure design feedback

200 views
Skip to first unread message

Jason Marmon

unread,
May 14, 2015, 7:56:12 PM5/14/15
to clo...@googlegroups.com

I'm working on a tool to orchestrate the migration from mongodb to datomic, and i'm looking for a bit of design feedback as i'm new to clojure. 


The user needs to provide functions that transform mongodb documents into datomic txes. Do y'all prefer the top or bottom style, or think there's a different way to implement this design that might be better?

12345678910111213141516171819
;; Multimethod for transforming data based on the name of the source mongodb collection
(defmulti transform identity)
 
(defmethod transform "events" [data]
;; do something with data
)
 
(defmethod transform "users" [data]
;; do something with data
)
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
(defprotocol Collection-Transformer
(transform [mongo-db document]))
 
(def user-transformer
(reify Collection-Transformer
(transform [m d] "do stuff")))

Max Countryman

unread,
May 14, 2015, 8:00:12 PM5/14/15
to clo...@googlegroups.com
I personally prefer multimethods, generally speaking—I think they feel more idiomatic. Although it does depend on the context of what you’re doing. In some cases a protocol may be the correct choice. Here the multimethod seems fine.


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Compton

unread,
May 14, 2015, 8:36:03 PM5/14/15
to clo...@googlegroups.com
Alex Miller's recent blog post on this is quite relevant http://insideclojure.org/2015/04/27/poly-perf/

Jason Marmon

unread,
May 15, 2015, 3:04:03 PM5/15/15
to clo...@googlegroups.com
Thanks for the feedback guys. Another related Q: The user needs to require the namespace that those defmethods are defined in for the multi to know about it. Presumably each defmethods will be in individual files, meaning the user has to require all those files for the migration tool to work. Is there a cleaner way of make sure that the defmulti knows about those methods?

Luc Préfontaine

unread,
May 15, 2015, 6:07:58 PM5/15/15
to clo...@googlegroups.com
I regroup these name spaces in an api name space and require this name space where it makes sense.

You can cut out your APIs as you wish around one or more mutli methods and add
whatever stuff needs to be part of it.

Still manually managed but not scattered everywhere. Never thought of a different way
to manage this however.

Luc P.
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
--
Luc Préfontaine<lprefo...@softaddicts.ca> sent by ibisMail!
Reply all
Reply to author
Forward
0 new messages