Dependency injection – a functional way
gettingsharper.de
gettingsharper.de
With dependency injection, you lose referential transparency but you gain a much cleaner and more encapsulated approach.
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.