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

In our first part of the series we saw what Autofac had to offer us in order to get the simple console application that provides code completion capabilities.

In this part I’ll use StructureMap and we’ll see how this product does.

First let’s download it from http://sourceforge.net/projects/structuremap/files/. The latest version is StructureMap2.5.3.

StructureMap is configured through a static ObjectFactory class. Usually you end up creating such a global class yourself, at least it comes already bundled with it.

Because StructureMap uses the ObjectFactory class for initialization as well as object retrieval our Bootstrapper now looks like this.

 

public class Bootstrap
    {
        public static void Components()
        {
            ObjectFactory.Initialize( container =>
                {
                    container
                        .ForRequestedType<IClearScreen>()
                        .TheDefaultIsConcreteType<ConsoleClearScreen>()
                        .AsSingletons();
                    container
                        .ForRequestedType<IWriteString>()
                        .TheDefaultIsConcreteType<ConsoleWriteString>()
                        .AsSingletons();

                    container.ForRequestedType<IFunctionState>().AddConcreteType<AlarmFunctionState>();
                    container.ForRequestedType<IFunctionState>().AddConcreteType<NewLineFunctionState>();
                    container.ForRequestedType<IFunctionState>().AddConcreteType<CalculatorFunctionState>();

                    container.ForRequestedType<IConsoleInputService>().AsSingletons()
                        .TheDefault.Is.OfConcreteType<ConsoleInputServiceImpl>()
                        .WithCtorArg( "functionStates" ).EqualTo(
                            ObjectFactory.GetAllInstances<IFunctionState>()
                         );

                } );
        }
    }

On thing you will absolutely notice is that the way we configure it has changed, but it still uses a fluent interface for configuration. One other thing is that it nows to resolve registered instances into an array that can be injected with a constructor argument.

Next I’ll show the same Bootstrapper, but instead of defining it in code I’ll move the part I expect to change into an xml.

public class Bootstrap
    {
        public static void Components()
        {
            ObjectFactory.Initialize( container =>
                {
                    
                    //container
                    //    .ForRequestedType<IClearScreen>()
                    //    .TheDefaultIsConcreteType<ConsoleClearScreen>()
                    //    .AsSingletons();
                    //container
                    //    .ForRequestedType<IWriteString>()
                    //    .TheDefaultIsConcreteType<ConsoleWriteString>()
                    //    .AsSingletons();
                    
                    //container.ForRequestedType<IFunctionState>().AddConcreteType<AlarmFunctionState>();
                    //container.ForRequestedType<IFunctionState>().AddConcreteType<NewLineFunctionState>();
                    //container.ForRequestedType<IFunctionState>().AddConcreteType<CalculatorFunctionState>();

                    container.PullConfigurationFromAppConfig = true;                    

                    container.ForRequestedType<IConsoleInputService>().AsSingletons()
                        .TheDefault.Is.OfConcreteType<ConsoleInputServiceImpl>()
                        .WithCtorArg( "functionStates" ).EqualTo(
                            ObjectFactory.GetAllInstances<IFunctionState>()
                         );

                } );
        }
    }

The PullConfigurationFromAppConfig flag tells structure map to read the configuration file.

Caution: I had to move the IClearScreen and IWriteString to the config file as well since it doesn’t know how to configure the IFunctionState items otherwise.

For this simple example I used the App.config file, you can however configure everything in a StructureMap.xml located in the same directory you would normally put your App.config.

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="StructureMap" type="StructureMap.Configuration.StructureMapConfigurationSection,StructureMap"/>
    </configSections>
    
    <StructureMap defaultAssembly="IoCContainers">    
        <DefaultInstance
                PluginType  ="IoCContainers.Components.Services.IWriteString, IoCContainersStructureMap"
                PluggedType ="IoCContainers.Components.Services.ConsoleWriteString, IoCContainersStructureMap"
                 Scope="Singleton"  />
        <DefaultInstance
                PluginType  ="IoCContainers.Components.Services.IClearScreen, IoCContainersStructureMap"
                PluggedType ="IoCContainers.Components.Services.ConsoleClearScreen, IoCContainersStructureMap"
                 Scope="Singleton"  />
                 
        <AddInstance Key="Alarm"
            PluginType  ="IoCContainers.Components.Functions.IFunctionState, IoCContainersStructureMap"
            PluggedType ="IoCContainers.Components.Functions.AlarmFunctionState, IoCContainersStructureMap"  />
        <AddInstance Key="NewLine"
            PluginType  ="IoCContainers.Components.Functions.IFunctionState, IoCContainersStructureMap"
            PluggedType ="IoCContainers.Components.Functions.NewLineFunctionState, IoCContainersStructureMap"  />
        <AddInstance Key="Computer"
            PluginType  ="IoCContainers.Components.Functions.IFunctionState, IoCContainersStructureMap"
            PluggedType ="IoCContainers.Components.Functions.CalculatorFunctionState, IoCContainersStructureMap"  />
        
    </StructureMap>
</configuration>

Now we can change values without recompiling the application, this however is error prone, and does not handle class renames to lightly. It’s not big of a deal if you think of it since you know you have it configured so when you rename a class you’ll know to go and rename values from the config file as well.

That was it for know, I’ll choose another IoC for our next post, the code you can get from here.

Posted on Sunday, August 16, 2009 1:03 PM | Back to top


Comments on this post: IoC in .NET part2: StructureMap

# re: IoC in .NET part2: StructureMap
Requesting Gravatar...
Hey, just a note on SM version, the latest I can see there is 2.5.3, and I don't see 2.3.1 anywhere - maybe just a typo?
Left by Marek Rosa on Aug 17, 2009 7:45 AM

Your comment:
 (will show your gravatar)


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