Search
Close this search box.

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 AutofacStructureMapNinject 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

Related Posts