5 ms·
Dependency injection – a functional way
- gloryless 12y agoAfter "defining module:" the page doesn't load.
- jmartinpetersen 12y agoWorks for me.
- cssmoo 12y agoSame trouble. Android Chrome.
- zak_mc_kracken 12y agoUsing `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 12y agoYou 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.
- mercurial 12y agoThat'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.