
Dependency injection – a functional way - numo16
http://gettingsharper.de/2015/03/10/dependency-injection-a-functional-way/
======
mercurial
That's why functors (in the OCaml sense) are awesome. They let you parametrize
a module over a module signature. It's really good if you know your
dependencies at compile time. If you don't, you can just use first-class
modules to generate the module hierarchy on the fly, though there is a small
syntactic cost.

------
zak_mc_kracken
Using `Reader` is not dependency injection, it's dependency passing. It also
has the downside of polluting all your function signatures and it exposes all
the internal dependencies of a function to all its callers.

With dependency injection, you lose referential transparency but you gain a
much cleaner and more encapsulated approach.

~~~
tel
You can pretty easily extend this with `mtl` in Haskell

    
    
        genericInjected :: MonadReader Handle m => m ()
    

If you use lenses you can express an even nicer pattern

    
    
        genericInjected :: HasHandle Database m => m ()
    

Perhaps the king here is ML's functors

    
    
        module ( Dbh : DatabaseHandle ) = struct
          ...
        end
    

In all cases this isn't much more than "dependency passing", but there is
clearly more "injectability" and encapsulation available. Ultimately it all
comes down to the kind of typed interface you want to expose.

------
gloryless
After "defining module:" the page doesn't load.

~~~
cssmoo
Same trouble. Android Chrome.

