Home > View Post

The Awesome Power of IoC

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 and 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.

using System;

namespace Tjoc.MusicalIoc
    public interface IPercussion
        void Hit();

Then you'll want a concrete implementation, something like the one below. Notice that we've opted to use the SnareDrum this time.

using System;

namespace Tjoc.MusicalIoc
    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:

            service="Tjoc.MusicalIoc.IPercussion, Tjoc.MusicalIoc"
            type="Tjoc.MusicalIoc.SnareDrum, Tjoc.MusicalIoc">

Finally, in the console application's Main method:

using System;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;

namespace Tjoc.MusicalIoc
    class Program
        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.

Tags: .NET

Josh Post By Josh Twist
1:01 PM
20 Sep 2006

» Next Post: The Awesome Power of IoC Part II
« Previous Post: Ultimate accolade

Comments are closed for this post.

Posted by Ivan Porto Carrero @ 20 Sep 2006 1:38 PM
Hi Josh,

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 :)

Posted by Josh @ 21 Sep 2006 4:31 AM
Thanks Ivan,

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!

© 2005 - 2022 Josh Twist - All Rights Reserved.