Jim Snavely on 24 Jul 2012 18:16:40 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: followup to PhillyETE: dependency injection without the gymnastics (tony morris on monads)


The most common dependency injection solution I've run into is Spring with POJOs. Typically the Spring framework uses setter methods, although it possible (but less common) to make Spring invoke a constructor with arguments.

I'm looking forward to reading Tony's slides - so I don't know yet why he prefers a monadic solution to traditional DI frameworks. One reason I can think of: DI frameworks rely on [semi-global] mutable state, which destroys referential transparency.

The second reason is that you can use different monads to establish different levels of access - use the Reader monad when you only need read-only access to the application context, use the State monad when you need read/write. Furthermore, this access will be enforced by the type system - so you fail fast (at compile time) instead of waiting for runtime errors.


On Tue, Jul 24, 2012 at 7:07 PM, Dustin Getz <dustin.getz@gmail.com> wrote:
At PhillyETE, Tony Morris gave a talk "dependency injection without the gymnastics"[1]. (To establish a common language: "dependency injection" is when we structure our functions to take dependencies by argument, instead of hardcoding them, so we can swap implementations e.g. to mock them.)

Tony's thesis is that "injecting dependencies by argument is cumbersome" and "traditional enterprise dependency injection solutions are shitty" (this is questionable to me?) and he goes on to provide monadic Scala code to solve this problem. I've reimplemented it in python to better understand, and I question his assertion, and don't really see the value-add of pass-by-arguments because deeper in the callstack when we get to business logic, we have to pass dependencies by argument anyway.

to help understand what he's talking about, here is a 30 line python program that demonstrates using reader-monad to inject dependencies per Tony's talk. (way easier to comprehend than the wall of code in the slides): https://gist.github.com/3172127#gistcomment-379014

what approaches to DI have you guys seen in the wild? any thoughts as to why Tony asserts that a monadic solution is superior to traditional solutions? i'm really not seeing the value-add here, but he's a smart guy and quite assertive about the superiority of a monadic approach.




--
--Jim