Geeks With Blogs
Sharpoverride The road to becomming a good programmer is paved with bad scripts

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. Posted on Monday, August 17, 2009 5:32 AM | Back to top


Comments on this post: IoC in .NET part 5: Using CastleWindsor container

# re: IoC in .NET part 5: Using CastleWindsor container
Requesting Gravatar...
There is in my opinion a much better way to configure Windsor and that's by using the fluent interface. Check this link for an example
http://hammett.castleproject.org/?p=250
There are lots of examples, just search for them.
Thx, Jan
Left by Jan Stenberg on Aug 18, 2009 7:43 AM

# re: IoC in .NET part 5: Using CastleWindsor container
Requesting Gravatar...
U can even register several components at the same time, like this:
wc.Register( Component.For<IWriteString>()
.ImplementedBy<ConsoleWriteString>()
.LifeStyle.Singleton,
Component.For<IClearScreen>()
.ImplementedBy<ConsoleClearScreen>()
.LifeStyle.Singleton
)
Left by Jan Stenberg on Aug 19, 2009 12:20 PM

Your comment:
 (will show your gravatar)


Copyright © Sharpoverride | Powered by: GeeksWithBlogs.net | Join free