IoC in .NET part2: StructureMap

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

} ); } }

(http://11011.net/software/vspaste)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 //.TheDefaultIsConcreteType //.AsSingletons; //container //.ForRequestedType //.TheDefaultIsConcreteType //.AsSingletons;

//container.ForRequestedType.AddConcreteType; //container.ForRequestedType.AddConcreteType; //container.ForRequestedType.AddConcreteType;

container.PullConfigurationFromAppConfig = true;

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

} ); } }

(http://11011.net/software/vspaste)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.

<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" />

(http://11011.net/software/vspaste)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.

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.