views:

215

answers:

4

I often read about dependency injection and I did research on google and I understand in theory what it can do and how it works, but I'd like to see an actual code base using it (Java/guice would be preferred).

Can anyone point me to an open source project, where I can see, how it's really used? I think browsing the code and seeing the whole setup shows me more than the ususal snippets in the introduction articles you find around the web. Thanks in advance!

A: 

I think dependency injection has a way of disappearing from view if used properly, it will be just a way of initializing/wiring your application -- if it looks more fancy than that you are probably looking at extra features of the framework at hand, and not at the bare-bones dependency injection.

Edit: I'd recommend actually starting to use it instead of trying to find examples, and then come back and post questions here if you can't get stuff to work like you'd think it should :-)

Simon Groenewolt
This is just an excuse not to have any good samples out. Seriously, with the size of the Java community, there's no one out there teaching this stuff in the open source space?
Matt Hinze
For real 'teaching' I'd recommend getting a book (I did for learning about spring). My point was merely that dependency injection is not such a big idea that is it really demonstrated by one special application.
Simon Groenewolt
+2  A: 

I struggled a bit with this exact issue. It's so abstract and simple I was always worried I was "doing it wrong".

I've had been using it in the main project which has dependencies on other projects because the Guice module which sets the bindings was part of the main project.

I finally realized the libraries should be supplying the Modules themselves. At that point you can depend only on an instance of a Module (not a specific one), and the interfaces that are bound by it.

Taking it one step better, you can use the new ServiceLoader mechanism in Java 6 to automatically locate and install all Guice modules available on the classpath. Then you can swap in dependencies just by changing class path (db-real.jar vs. db-mock.jar).

Mark Renouf
+1  A: 

I understand you're in Java-land, but in the .NET space the are several open-source apps written using an inversion of control container. Check out CodeCampServer, in which the UI module doesn't have a reference to the dependency resolution module. There is an HttpModule that does the work. (an HttpModule is just a external library you can plug in that handles events in ASP.NET, in CodeCampServer the UI project loads this DependencyRegistrarModule at run time, without any compile time reference to it.)

Matt Hinze
Thanks will look at that, even if it's not Java it's something real to look at.
André
+4  A: 

The Wave Protocol Server is my favourite example app.

Jesse Wilson