Wednesday, April 16, 2014

5 Faces of Dependency Injection in Clojure

Dilemma:

Working together
How, in a functional way, to access resources like database and other external dependencies.  Most non-trivial applications will have one or more external resources that need to be acquired in order to work. Things like databases and web services will fall into this category.  The problem is they need to be shared across much of the code base but they are not truly static.  The database password will need to change.  Different test and production environments will need to point to different servers.

How to write the code in a way that allows for the database information to be different each time the code is run?  If this were Java, the common answer is Dependency Injection.  Using the Spring Framework, dependencies are injected into the objects at run time.  This allows for the configuration to happen outside the code and still let the objects have access to it.

Solution:

Thanks to Stuart Sierra for his simple and elegant context idea as demonstrated in his Clojure in the Large presentation as well as his Component library.  Using the techniques he describes, it is possible to separate the configuration of the resources from the code that needs them. This addresses the first part of the problem.  The second part remains namely: how to give the code access to the resources.

Thus leading to the title: Dependency Injection in Clojure.  For object oriented languages like Java, Dependency Injection is a powerful technique and widely used.  Of course, Dependency Injection techniques are not meaningful in a functional environment.  It requires objects and specifically mutable objects to really work.  The objects are built and then mutated by having their dependencies injected.

However, the same problem exists in Clojure.  Rather than giving objects access to the context, the Clojure problem is giving functions access to the context.  For this article, Dependency Injection will have a looser definition meaning "giving functions access to the context".

As I have thought about it and was able to discuss it with Ben and Levi at Lambda Lounge Utah, there are at least 5 ways of doing Dependency Injection in Clojure.  That is, 5 ways of giving functions access to a shared context.  Rather that present them in order based on obviousness, they will be presented starting with the most naive.

Globally Shared

This is often the first way developers think to share data across an application: simply throw it in a def in a namespace and allow any function that needs it to reference it from there.  


It has the advantage of being simple to implement.  The disadvantages are numerous and Dependency Injection was originally developed to overcome the shortcoming of globally shared data.  Among other things, putting the context in a globally shared data structure will make testing and moving across environment much more cumbersome.

Binding

The next thing people will often try is using the Clojure binding form

This seems to overcome the global shared problem as now a function can access its copy of the form.  However, Be Mindful of Clojure's binding.  This is an excellent article which describes some of the perils of using the binding form.  It specifically mentions crossing thread boundaries and problem involving lazy sequences.  While your context most likely won't be lazy, it will almost certainly be crossing thread boundaries making the use of binding possibly troublesome.

Both the previous examples use dynamic scoping which is the underlying trouble.  It would be preferable to using lexical scoping to access the context.  The remaining 3 examples will use lexical scoping.

Function Argument

The first example of lexical scoping is simply passing the context into each function that wants it as an argument.

There is a lot to say for this method and will be the first choice of dependency injection in Clojure.  In a majority of cases, this method will serve nicely and makes for nice clean code.  However, there is at least one drawback that a simple example does not reveal.  The context ends up getting passed to functions that do not use it directly, but just pass it on to functions which they in turn call.  If you think of a large project, there may be hundreds of functions all passing around the context just so it can make it way to the bottom layer of code which interacts with the resources.  It might be my OO upbringing but this feels like exposing implementation details just as leaky abstraction does in Java.

Closing over the Context

Another method would be closing over the context using let and letfn.

In the example the context is passed into a function which then closes over it.  The foo function is no longer visible outside the closure and can only be called by the app function.  While one could write the entire application in such a fashion, it is not very clear.  Where this example shines is in the case of callbacks, like in Swing.  The function can close over not only the context but the components of the application as well.  Making it quite simple to create call backs that reference not only the context but the components.

Reader Monad


Monads often get a bad wrap, but for good reason.  They are a bit hard to grasp. The examples and tutorials often describe what they are but not what to do with them.Fortunately I found a good example in the presentation "Monads in Clojure" by Leonardo Borgess.

Let me describe what the reader monad will do, but not how it does it (as I do not really care how it works).  Looking at the Closing over the Context example above, the function takes a context, creates the clojure and returns a function that will execute the functions with the symbols bound.  But, what if that we turned inside out?  Rather than closing on the context, close on what other data is needed at creation and return a function that expects the context as an argument.


This requires an explanation.  What does (domonad reader-m [val func] body) do?  Quite simply, it returns a function that accepts a single argument, the context.  When that function is executed with the context as an argument, func is executed and the result stored as val.  The body is then executed and returned.

The func function has to return the monad as well so it can be called with the same context passed in to the calling function.

In foo there is a function asks.  This function is a helper for reader monad that returns a value from the context.


What happens app function is called?  It returns a function that behaves like this

Of course, it does not really use defn but uses macros to generate the required functions and return the top level one.  When app* is executed, it then calls foo* passing in the context where it can be executed.  The nice thing is there can be any number of function calls between app and foo that know and care nothing about the context.

Of course the (domonad reader-m is hard to read and really does not really convey intent very well.  To help make it clearer, I created some simple macros to wrap the code resulting in this code.

The wrapper is at topoged/context.clj if you care to look.  The advantage is that now the context can be passed using lexical scoping without having to specifically mention it in the argument lists of functions that do not need it.  The downside is you have to know which functions return the monad and which don't.  Those that do have to be in the [val func] bindings while those that don't have to be in the body. This might end up being a pain as well.  While I think this option shows promise, that will need to be overcome to make it truly useful.

The 5 methods of dependency injection, or getting data into functions, are: globally shared, binding, function arguments, closing over the context and the reader monad.  Each has a place in the whole architecture of a Clojure app and should be in every developers toolbox.


Wednesday, March 5, 2014

Using malabar-mode to auto-populate a new Java file

Java has a lot of "template" code that needs to be written and most editors and IDEs understand this and help alliviate the common tasks.  One of these is the putting a skeleton into a blank file.  To do this in malabar-mode, there is the malabar-codegen-insert-class-template function that will:
  • add the class declaration based on the name of the buffer
  • add a javadoc comment skeleton
  • update the package statement
To have EMACS run this function when a new file is created, add the following to your .emacs:

;; Auto-populate an empty java file
(add-hook 'malabar-mode-hook
      '(lambda ()
         (when (= 0 (buffer-size))
           (malabar-codegen-insert-class-template))))

Tuesday, March 4, 2014

malabar-update-package now works in Windows

The functions malabar-update-package is used to add or fix the package statement in a Java class based on the location of a file in the maven source tree.  For example running malabar-update-package in a buffer looking at  src/main/java/com/m0smith/Test.java will result in the package being set to  package com.m0smith;

Wednesday, February 26, 2014

lein-resource 0.3.4 adds timestamp checking

The latest release of lein-resource, 0.3.4, adds support for a new flag, :update which when set to true checks time stamps before processing source files.  This provides a way to remove unnecessary copying and filtering if the source file has not been modified.

While it could potentially save time, it should not be used if build sensitive stencil filtering is used on any of the source files.  For example, if the source contains a {{timestamp}} tag then setting :update to true would prevent the file from being recreated on each build.  The suggestion therefore is to only set :update to true when :skip-stencil is also true.

The default is false or to always overwrite the destination file.

Monday, February 3, 2014

maven-pom-mode: Add Dependency

The maven-pom-mode project has been updated to have its own mode.  It will also search for and add a dependency

* `C-c d` will Search for artifacts in maven central and insert dependency tag via `maven-pom-add-dependency`.  NOTE the point has to be in the right place in the pom file when called.

Friday, January 31, 2014

malabar-mode 1.6 Change Log

Change log

Resolved Issues



  • "Apply suggestions to flycheck"
  • "import not work well in Win NT"
  • "Default to -source 1.5"
  • "in malabar-flycheck-error-column assign tabs to 8 spaces."
  • "Remove the @ from the front of annotations in malabar-import-one-class"
  • "File mode specification error: (void-function wisent-java-tags-wy--install-parser)"
  • "Compile error in melpa"
  • "Compile File Issue: IllegalArgumentException"
  • "Cannot open load file: wisent-comp"
  • "Show super classes and implemented interfaces of a class"
  • "Add pom.xml mode of some sort"
  • "[feature-request] malabar-delegate-interface"
  • "Can C-c C-v C-y be made to work for source jars?"
  • "Some sort of xref facility"
  • "Flymake/Flycheck"

  • Commits

    Tuesday, January 28, 2014

    malabar-mode meet flycheck-mode

    malabar-mode is an EMACS major mode for editing Java files in a MAVEN project.  flycheck-mode  is a modern on-the-fly syntax checking extension for GNU Emacs 24.   The latest release of malabar-mode in MELPA (20140128.1331) has integrated flycheck.

    To enable:
    (load "malabar-flycheck")(flycheck-mode)
    Once enabled, it uses javac to compile the buffer as changed.  See flycheck-mode for usage.

    Monday, January 27, 2014

    Introducing maven-pom-mode

    As part of my work on malabar-mode it has become apparent that EMACS needs a maven pom mode.  Looking around the various maven pom related projects, there are a lot of good ideas but not one project that incorporates them all.  The maven-pom-mode was created to give EMACS a better way of working with your pom files.

    Current functionality

    EMACS has built in XML editing via nxml-mode which includes tag completing, listing valid tags, etc.  With maven-pom-mode the schema for editing pom files is made available  to nxml-mode.

    •  C-M-i give valid suggestions. 
    • C-c / will close a tag.
    • C-c i will complete an inline tag.
    • C-c b will complete an block tag.

    Installation

    From the shell:
    git clone https://github.com/m0smith/maven-pom-mode.git
    
    In Emacs:
    (add-to-list 'load-path "path to maven-pom-mode")
    (load "maven-pom-mode")

    Future


    Future directions will include:

    • Searching for dependency in central and adding a dependency
    • Show and manipulate transitive dependencies
    • Form based manipulate sections of the pom
    • Integrate with malabar-mode

    Tuesday, January 21, 2014

    lein-resource 0.3.3 hits clojars

    Added skip-stencil as an option.  This allows for files to be copied but not processed by stencil.  Instead they are copied as is.

    Usage:

    To configure lein-resource, add to project.clj
    :resource {
      :resource-paths ["src-resources"] ;; required or does nothing
      :target-path "target/html" ;; optional default to the global one
      :includes [ #".*" ] ;; optional - this is the default
      :excludes [ #".*~" ] ;; optional - default is no excludes which is en empty vector
      :skip-stencil [ #"src-resources/images/.*" ] ;; optionally skip stencil processing - default is an empty vector
      :extra-values { :year ~(.get (java.util.GregorianCalendar.)
                                       (java.util.Calendar/YEAR)) }  ;; optional - default to nil
    }
    
    If :resource-paths is not set or is nil, then it won't do anything

    Wednesday, January 15, 2014

    lein-resource "0.3.2" hits clojars

    This is old news really, but I noticed I failed to announce:

    [lein-resource "0.3.2"]
     

    A plugin that can be used to copy files from multiple source directories to a target directory while maintaining the directories. Also, each file will be transformed using stencil. The map that is passed to stencil contains a combination of:
    • The project map
    • The system properties (with .prop added to the name )
    • Additional values (currently only :timestamp)
    • Values set in the project.clj using :resource :extra-values
     

    malabar-mode reanimated

    Hi all,

    I have taken over maintenance of malabar-mode which is a Java major mode for EMACS.

    While it is a great project, it has suffered some bit rot over the past couple of years.  With help from others, it has now been reanimated and is a contributing member of  the EMACS ecosystem.

    Accomplishments 


    So far a couple of big improvements have been made.  First, it is now part of MELPA so it can be installed via the package manager.  This has made a lot of difference and certainly makes it easier to get started.

    The project has been split into 2 projects: malabar-mode and malabar-mode-jar.  The former contains the lisp code and is what gets packaged by MELPA.  The latter is the Java and Groovy code the helps make the magic happen.  This jar file is now being released via Sonatype to the maven central as
    <groupid>com.software-ninja</groupid>
    <artifactid>malabar</artifactid>
    By splitting the project into two projects it makes it easier to maintian and deploy.

    The project now works under cygwin, at least partially.  There is still some work to do in this area.

    Getting Involved

    If you see an problem or have an idea for an enhancement, go ahead and create an issue.  Also, pull requests are welcome and help move the code forward.