I remember way back in the 90’s when I finally had “the light bulb” go on about object oriented programming. You know “the light-bulb” I’m talking about -- that moment when suddenly you get it and it all becomes clear and it all makes sense and you have this thrilling sense of understanding and one-ness with your code and the new concept. It’s really fun.
For me and OOP it came in the middle of reading what is still my all-time favorite Pascal book, Borland PASCAL 7 Insider by Paul Cilwa. I can still remember where I was sitting when suddenly it all came together in my brain. It was very exciting.
And I was feeling that way again this week when I started doing Dependency Injection with the Delphi Spring Framework. It really came to a head when I realized that I can create useful and powerful units that have no code in their interface sections.
That’s right: no code in the interface section. No uses clause, no code, no nothing. Seriously – a unit that looks like this:
// a whole bunch of cool code here and nowhere else
Yeah, you heard me – nothing. And yeah, the cool code mentioned above works and is totally accessible. That fact alone should give you pause – and give a clue into the power and coolness of what I’m talking about. And yes, I’m still giddy about all this.
So, how does this happen you ask? Well, it’s going to take a little while to explain – maybe even a few blog posts – so if you want to see this cool thing that’s got me all goose-pimply, you’ll have some to do some reading.
At the start, I want to say that I’m well aware that Java guys in particular have been doing this kind of thing for while, and that .net guys have been as well. That’s great. I know Delphi is a bit late to the party here, and I am too, but that doesn’t make it any less cool and it doesn’t make me any less giddy.
Okay, first, I’ll give you a few things to do. To get the basic idea of where we’re going with all this, I recommend that you read the seminal article by the inestimable Martin Fowler about Dependency Injection. Understanding what Dependency Injection is is key to all this cool stuff. I’ll try to explain what Dependency Injection is in the first post, and then move on to the code itself.
Second, you’ll need at least Delphi 2010 and preferrably Delphi XE to do all this. This stuff requires Parameterized Types (Generics) and you can only get that language feature from Delphi 2010 and above.
Third, you should go and download the Delphi Spring Framework from Google Code. I recommend that you pull it using Subversion instead of grabbing the zip files as then you’ll be able to keep up more easily on this always improving project. Get the code and start poking around, particularly in the Spring.DI.* units and the tests that go with it. The TContainer class is really the heart of things, and so give that a close look.
And if you want some cool videos to watch that talk about a number general principles of how you can use dependency injection to make your code better, I’d recommend these three by Misko Hevery of Google:
And that ought to get you started thinking about some of the things you should be thinking about when doing Dependency Injection.
Next time I’ll give some samples of code and why this is all important. And yes, I’ll likely be using a lot of the same material that Misko did.