You are currently browsing the Philipp Meier's weblog posts tagged: ioc


forum

Dependency Injection is not only about compile time dependencies

Tinou complains that dependency Injection was broken. I want to add some points to his / her statements:

The problem I’ve always had with DI frameworks, be it Spring or Guice, is they create this nasty dependency tree. If you don’t want to use GlobalApplicationContext.getBean() or Injector.getInstance() then you’ll need to inject all your dependencies at the root. It annoys the crap out of me, but I suppose there’s just no way around it…
Except if the language had a mechanism to realize interfaces and abstract classes at runtime, either built-in or through some extension.
[…]The fundamental problem with all dependency injection tools is they are trying to do what language should be doing (instantiating objects that implement some interface[!]).

These toughts might be correct if you reduce dependency injection to “instanciate a class that implement this or that inferface”. But I understand dependency injection as a tool for inversion of control and decoupling of implementations. Dependency injection shall enable the developer to reconfigure the application without compilation of the compontents that depend on eachother.

Yes, at some point you will have to decide which implementation of a certain interface shall be used, and one can argue where this “wiring information” should be placed: Spring uses an XML file, PicoContainers can be composed hierachically, guice can use annotations. But if you have only one implementation of the interface you can do without a dependency injection framework at all.

Updates

  • Fixed typos
  • As pointed out by Paul Hammant PicoContainer of course can be configured with XML as well as with Groovy, Python, Beanshell and Ruby. I’m sure one can add Yaml and Json easily as well. Furthermore I think this is possible to some extend for Spring and Guice as well and the Qi4J developers would be able to add something to this, too. What I wanted to make clear is that one can argue about when, where and how to wire the components; the different ways the mentioned frameworks offer emphasize this.
service

Where is inversion of control in JSR 296 (Swing Application Framework)?

JSR-296 will provide us with nice ressource management. You can get property settings like label texts, colors and other properties from ressource files easily:

ApplicationContext ctxt = ApplicationContext.getInstance();  
ResourceManager mgr = ctxt.getResourceManager();
resource = mgr.getResourceMap(HelloWorld.class);
String helloText = (String) resource.getObject("helloLabel", String.class);

But… what is this ApplicationContext.getInstance() static method call? Where is dependency injection? Look into the API documentation of ApplicationContext: it’s a global service locator. That means it provides a fixed bunch of services which are accessible by a singleton instance. You can of course use aggregation, sub-classing and delegation to extend it’s abilities but dependency injection is the train everyone jumps onto, ain’t it?

There is another issue that puzzles me: where are the interfaces? A framework whithout any interface? It’s even worse, ApplicationContext returns instances of concrete classes, not even abstract classes. So you’ll always get an instance of class RessourceManager. I you like to provide your own, you can only sub-class it, at least, RessourceManager is not final.

Can I have some dependeny injection, please? Picocontainer, Guice or Spring, perhaps? Yes, I can but it would be more nice, if RessourceManager, ActionManager and all other services provided by the framework would be interfaces and I could use any implemention I wish.

By the way, there is some dependency injection in the framework: you can inject property values into any awt component with RessourceManager.injectComponent. I hope the method name is not final yet, because I expected an component to be injected into some other instance, not something to be injected into a component.

report
participate

Bear