OK, I'm probably a little bit late in the game with this but I've recently been playing with some of the produce of the Castle Project
. In particular, I've been looking at their Windsor Container
and I think I'm finally starting to get it.
Maybe there are more people out there who haven't had the chance to play with this yet or who, like me, failed to realise the value of this concept at first. So I'm going to write a small series of posts to take you on the same little journey I've just enjoyed (it only took a couple of hours, literally on the train too!)
but first, some background
... I was going to put lots of background on IoC (Inversion of Control), dependency injection and Martin Fowler here. But I'll just be stealing most of it from Wikipedia and Martin et al. Instead why not just nip off and read this
. Phew, that saved me a bit of typing (ahem... CTRL+C & CTRL+V)
Now, a quick example
For the purposes of this example I'll be using Castle's Windsor Container so I recommend you head off and download the MSI
and get it installed. This puts all the necessary assemblies in your GAC, ready to play with.
First, create a new console application project (I called it "Tjoc.MusicalIoc") and add references to Castle.MicroKernel, Castle.Model and Castle.Windsor.
Next we need to create an interface, something like the one below. Programming with interfaces is good and nice like squirrels and candy floss.
public interface IPercussion
Then you'll want a concrete implementation, something like the one below. Notice that we've opted to use the SnareDrum this time.
public class SnareDrum : IPercussion
public void Hit()
Simplistic but you get the idea, I'm sure. Next we need to configure our component with a simple xml file called Config.xml, like this:
Finally, in the console application's Main method:
private static WindsorContainer _container;
public static void Main()
// load up the configuration file to create a new container
_container = new WindsorContainer(new XmlInterpreter("../../config.xml"));
// get a new IPercussion service
IPercussion drum = (IPercussion)_container["snareDrum"]; // could also use container[typeof(IPercussion)] to get default
drum.Hit(); // guess what happens here!!!!
Did you guess? Yes, well done.
Excited? No, not really.
Looks fun but hardly worth that much fuss is it?
Well that's what I thought, but in the next post we'll make it a bit more exciting. But only a bit, we don't want you to explode or anything.
UPDATE: Part II now available.
UPDATE: Part III now available.
20 Sep 2006
» Next Post:
The Awesome Power of IoC Part II
« Previous Post:
Comments are closed for this post.
Ivan Porto Carrero
20 Sep 2006
Firstly thank you for the great blog you provide.
Next I guess I'm as late as you are to get into the IoC madness.
I started playing with the castle project yesterday.
I'm just glad I'm not the only one to join in this late :)
21 Sep 2006
I suspect there's lots of people who will be even later to the party. This industry just moves so darned fastg it's impossible to keep on top of everything!
Let me know if we miss any pearls over the next few posts!