Hi everyone, i’m learning basic web development with clojure. I have some question.
In luminus template, they use mount or component library to manage db connection and I see many people use it too.
Why should i use mount and what is common scenarios to use it.
Using mount or one of the other libraries for stateful components is not strictly necessary. What they give you is a way to tear down and rebuild the stateful parts of your application without restarting the REPL. You might have seen this old blog post by Stuart Sierra on the reloaded workflow, which inspired these systems: http://thinkrelevance.com/blog/2013/06/04/clojure-workflow-reloaded
I usually encapsulate the database connection pool in one component, the web server in another, and declare the database component a dependency of the web server component.
I think I saw another topic here on Clojureverse with some discussion on the pros and cons of component systems. Some alternatives were also highlighted in that topic, e.g. using delay for encapsulating components.
First of all, I’d use integrant. I’ve worked in production with all three and integrant is the most capable and overall pleasing to work with.
Yes, these state management libs are not strictly necessary, but they let you define each state independently and then combine them somewhere else into a system of dependencies. They take care of the order of starting and stopping the dependencies.
Integrant lets you define that system in a configuration file, and integrant-repl lets you read that configuration on REPL restart so you can quickly try different system topologies. For example you could define channels as states and pipe them with integrant, changing the piping, the buffer types, the processing functions etc.