Pure Danger Tech


navigation
home

Dependency Injection and Type Safety

27 Feb 2008

I’ve had a couple interesting discussions recently about dependency injection and type safety. In particular there is a new article up discussing type safety in the Butterfly DI framework. I think Jakob makes some good points and argues well that perhaps too much is made of the lack of type safety in Spring XML config. (And yes, I know you don’t have to do it in XML.)

Guice is perhaps the other end of the scale as the type safety of the configuration seems to be one of the guiding principles of Guice.

Personally, I find myself sympathetic to the benefits of static typing. I’ve worked on big systems (1M+ LOC) and have had these kinds of problems before; not with Spring in particular, but in general with code that references classes and methods by name rather than by a strong reference. Over the years, I’ve seen a number of little problems that cost me a tiny amount of time to find and correct – usually found quickly at runtime or in unit tests. And I’ve had big subtle problems a few times that cost me lots of time. Sometimes these are more subtle than just a bad class/method name and relate back to implicit typing of string vs numeric values or something like that.

To me, one of the things that Java is really good at it is static typing. If you’re going to use Java to build your system, especially Java 5+ with generics and annotations, you should leverage the heck out of it. Otherwise, why are you using Java anyways? My feeling is that Guice was created in this spirit.

But I see all of these choice as valid options and to some degree a matter of preference on a continuum. Where do you fall? How much static typing do you like in your DI?

Originally posted on the Java Zone.