Dead Ink Vinyl

Musings of David L Kinney

Getter-Based Injection Sucks

Cedric is back from vacation and has posted his continuing thoughts on getter-based dependency injection. He was somewhat too self-congratulatory in the posting, proclaiming his idea to be “the best of both worlds”—the best of constructor-based injection and setter-based injection. Unfortunately, he was very wrong. I tacked on a comment to his posting and I am extending it here.

It is not the best of both worlds. Cedric skims over the most important detail for testing. He off-handedly says “All you need to do is provide a default implementation for your getter…” to be able to test the classes. How does he think developers will implement that “default” functionality? It will be performed by either constructor-based injection or setter-based injection!

So with Cedric’s getter-injection, developers still have to implement the exact environment Cedric was trying to avoid plus he has ensured that unit tests won’t accurately reflect how the classes will behave in their container.

I would have to say that it is the worst of both worlds. Sorry, Cedric.

Written by dlkinney

June 3, 2004 at 7:25 pm