Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functional dependency injection

When writing object-oriented software, I use dependency injection a lot:

  • to compose together high-level functionality from lower-level capabilities: my account management service uses repositories and validation services rather than implementing them itself.

  • to isolate components from their dependencies: my account management service uses its dependencies through interfaces, so that I can swap implementations, mock for unit testing and so on.

What patterns exist in functional programming languages to achieve these goals?

edit: a commenter rightly asks: "what about just passing round functions?". I think that the following comment about function grouping hits the nail on the head - a service is a collection of functions with a shared set of dependencies that I can handle as an atomic group.

In Clojure it seems like protocols solve this nicely, but I was really wondering how the problem is solved more generally...

like image 325
Dan Vinton Avatar asked Jun 22 '11 10:06

Dan Vinton


2 Answers

Some time ago I've read a post describing how dependency injection can be seen as currying in functional programming. I think it's very interesting, and it gives a good perspective on the topic.

like image 115
MarcoS Avatar answered Oct 20 '22 23:10

MarcoS


At the small scale, things like currying and functions-as-parameters cut down the need for module dependencies. At a larger scale, things like Standard ML functors are very useful for this purpose. Racket has a system called units that does a good job on this too.

like image 30
jacobm Avatar answered Oct 20 '22 23:10

jacobm