CLOJURE module dependencies

I am trying to create a modular application in clojure.

Let’s assume we have a blog engine which contains two modules, for example – Database module and article module (something that stores blog posts), all modules have some configuration parameters.

So – article module depends on storage, and has two article modules and database modules (with different parameters) ) Allows us to host two different blogs in two different databases.

I try to achieve this to create a new namespace for each initialization module on the fly, and define this naming using some of the applied parameters Function in space. But I think this method is some kind of hacking.

What is the correct method?

“Module” is a noun, such as “Kingdom of Nouns” by Steve Yegge.

< /p>

Except at the highest level of abstraction, stick to the non-side effects or pure functions of its parameters (verbs) as much as possible. You can organize these functions at will. At the top level you will have some application state, there are There are many ways to manage it, but the most commonly I use is to hide these top-level services under the clojure protocol, and then implement it in the clojure record (may hold a reference database connection or some (for example).

This This method maximizes flexibility and prevents you from writing yourself into the corner. It is similar to java’s dependency injection. Stuart Sierra recently had a good discussion on these topics at Clojure/West 2013, but the video has not yet been announced .

Please pay attention to the difference with your method. You need to separate the management and solution of the object from its life cycle. Binding them to the namespace can be quickly accessed, but it means whatever you write Functions as clients that use this code are now accessing the global state. Using the protocol, you can separate the implementation details of the global state from the access interface.

If you need an motivating example to illustrate why this is useful , Please consider, how do you intercept all access to globally accessible services? Well, you will push the complete implementation and make the entry point a wrapper function instead of pushing the relevant details to the client code. If you want the code What about certain behaviors of some clients but not others? Now you are stuck. This is just looking forward to making the inevitable trade-offs in a preemptive way to make your life easier.

I am trying to create a modular application in clojure.

Let’s assume we have a blog engine which contains two modules, for example – database module and Article module (something that stores blog posts), all modules have some configuration parameters.

So – the article module depends on storage, and an instance with two article modules and a database module (with different parameters) is allowed We host two different blogs in two different databases.

I try to implement it to create a new namespace for each initialization module on the fly, and use some of the applied parameters to define the functions in this namespace .But I think this method is some kind of hacking.

What is the correct method?

“Module” is a noun , Such as “Kingdom of Nouns” by Steve Yegge. < p>

Except at the highest level of abstraction, stick to the non-side effects or pure functions of its parameters (verbs) as much as possible. You can organize these functions at will. At the top level you will have some applications State, there are many ways to manage it, but the most commonly I use is to hide these top-level services under the clojure protocol, and then implement it in the clojure record (may hold a reference database connection or some (for example).

< p>This approach maximizes flexibility and prevents you from writing yourself into the corner. It is similar to java’s dependency injection. Stuart Sierra recently had a good discussion on these topics at Clojure/West 2013, but The video has not yet been announced.

Please note the difference with your approach. You need to separate the management and resolution of the object from its life cycle. Binding them to a namespace can be quickly accessed, but it means you Any function written as a client using this code is now accessing the global state. Using the protocol, you can separate the implementation details of the global state from the access interface.

If you need an incentive example Why is this useful, please consider, how do you block all access to globally accessible services? Well, you will push the complete implementation and make the entry point a wrapper function instead of pushing the relevant details to the client code. What if you want certain behaviors of some clients of the code but not others? Now you are stuck. This is just looking forward to making the inevitable trade-offs in a preemptive way to make your life easier.

Leave a Comment

Your email address will not be published.