gustavo-depaula / dpgurl

an expirable url shortener written in clojure
MIT License
28 stars 2 forks source link

Migrate DB functions to the protocols #1

Open diegoleonardo opened 3 years ago

diegoleonardo commented 3 years ago

Hi @gustavo-depaula , how are you?

I think you could migrate DB functions (such as "local-DB-insert-URL-into-DB", and so on) to protocols. This way, you don't need if statement to decide what implementation you will use. Also, it's the opportunity that you will have to study and implement libraries to building your app (such as component, integrat, and so on).

gustavo-depaula commented 3 years ago

Hello @diegoleonardo, I'm fine, thanks for asking. What about you?

I've been reading about clojure protocols and it seems like a good idea. One question I have is that is there a way to switch betwen the different implementations of the protocol without a relying if statements. I think the same applies for component. I could not find what integrat is, however.

Kind Regards

diegoleonardo commented 3 years ago

Hi @gustavo-depaula.

I'm fine too, thanks!

So, I had thought about this, and I think better begin with little steps. Maybe a multimethod solve this in a simple way. It's worst in performance than use protocols, but It's just irrelevant for this type of problem.

Some links to help: https://clojure.org/reference/multimethods https://www.braveclojure.com/multimethods-records-protocols/

But answering your question, in that solution proposed with protocols and integrant (or component), the if statement would stay just only in one place and would be used in the startup of your application to choose between in-memory or Redis implementation. That way, you wouldn't need the if statements inside de functions insert and so on.

That said, I believe that the steps to apply this approach are as follow:

link to integrant: https://github.com/weavejester/integrant

Let me know if I was clear and what you think about. =)

Kind regards