IoC in .NET part 5: Using CastleWindsor container

Hi, this is part 5 of our series on IoC containers in .NET. I’m only using them on a simple console application that provides autocompletion for the functions that the application exposes.

We started with an introductory article, then we used Autofac, StructureMap, Ninject 2 beta and Spring.NET.

In this article I’ll cover CastleWindsor a bit. These files can be found on sourceforge at the following address http://sourceforge.net/projects/castleproject/files/InversionOfControl/.

Like StructureMap it can be configured through code or from an xml configuration. I’ll first show the way to configure the app from code than I’ll go for the xml configuration option.

Using the configuration from code looks like this:

public class Bootstrap {

    public static IWindsorContainer Components()
    {
        WindsorContainer wc \= new WindsorContainer();
        wc.AddComponentLifeStyle<IWriteString,
            ConsoleWriteString\>( 
             Castle.Core.LifestyleType.Singleton );
        wc.AddComponentLifeStyle<IClearScreen,
            ConsoleClearScreen\>(
             Castle.Core.LifestyleType.Singleton );

        wc.AddComponent<IFunctionState,
            AlarmFunctionState\>();
        wc.AddComponent<IFunctionState,
            NewLineFunctionState\>();
        wc.AddComponent<IFunctionState,
            CalculatorFunctionState\>();            

        wc.AddComponentLifeStyle<IConsoleInputService,
            ConsoleInputServiceImpl\>( 
            Castle.Core.LifestyleType.Singleton );
        return wc;            
    }
}

As Jan Stenberg pointed out, there is a better way of configuring the container and it looks like this

WindsorContainer wc = new WindsorContainer(); wc.Register( Component.For<IWriteString>() .ImplementedBy<ConsoleWriteString>() .LifeStyle.Singleton ) .Register( Component.For<IClearScreen>() .ImplementedBy<ConsoleClearScreen>() .LifeStyle.Singleton )

        .Register(Component.For<IFunctionState\>()
            .ImplementedBy<AlarmFunctionState\>())
        .Register(Component.For<IFunctionState\>()
            .ImplementedBy<NewLineFunctionState\>())
        .Register(Component.For<IFunctionState\>()
            .ImplementedBy<CalculatorFunctionState\>())

        .Register(Component.For<IConsoleInputService\>()
            .ImplementedBy<ConsoleInputServiceImpl\>()            
            .LifeStyle.Singleton)
        ;

You get the container like so

var kernel = Bootstrap.Components();

And the thing I kind-a struggled with is that in order to construct my service I had to declare a dictionary

var parameters \= new Hashtable();
parameters.Add( "functionStates",
kernel.ResolveAll<IFunctionState\>() );

And pass it in when I resolved the service

var shortcutService = kernel.Resolve<IConsoleInputService>(parameters);

The fact the you have to specify the parameters outside the bootstrapping is a small inconvenient but we can live with it.

The xml part is a bit more simple to comprehend an it’s what you’ll prefer to use in most applications.

Now the Bootstrap class looks like this

public class Bootstrap {
public static IWindsorContainer Components() { WindsorContainer wc = new WindsorContainer( new XmlInterpreter() ); return wc;

    }
}

And we got the service resolution down to

var shortcutService = kernel.Resolve<IConsoleInputService>( );

Niiice, isn’t it? As always the source can be found at this adress.

This article is part of the GWB Archives. Original Author: Sharpoverride

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.