IoC Containers in .NET part 6: Unity Container

Hello, this is the 6th part of the IoC Containers series. This is the last container I’ll explore. I’ve been using it at work because it’s got the “Made by Microsoft” insignia and it seems the policy is that anything done at Microsoft is good for use. Bet that happens to you also.

I’ve downloaded what I hope is the latest version from.

Our Bootstrapper code is now

public class Bootstrap { public static IUnityContainer Components { IUnityContainer container = new UnityContainer; container.RegisterType<IClearScreen, ConsoleClearScreen>( new ContainerControlledLifetimeManager); container.RegisterType<IWriteString, ConsoleWriteString>( new ContainerControlledLifetimeManager );

container.RegisterType<IFunctionState, AlarmFunctionState>("Alarm"); container.RegisterType<IFunctionState, NewLineFunctionState>("NewLine"); container.RegisterType<IFunctionState, CalculatorFunctionState>("Calculator");

container.RegisterType<IConsoleInputService, ConsoleInputServiceImpl>( new ContainerControlledLifetimeManager );

return container;

} }

(http://11011.net/software/vspaste)The ContainerControllerLifetimeManager is Microsoft’s way of saying this is a singleton instance. If we don’t specify nothing then a new instance will be created always.

The service retrieve through a simple call to container.Resolve.

The container also supports xml configuration.

Next we’ll se how our App.config file looks like

<type name="Alarm" type="IoCContainers.Components.Functions.IFunctionState, IoCContainersUnity" mapTo="IoCContainers.Components.Functions.AlarmFunctionState, IoCContainersUnity" />

<type name="NewLine" type="IoCContainers.Components.Functions.IFunctionState, IoCContainersUnity" mapTo="IoCContainers.Components.Functions.NewLineFunctionState, IoCContainersUnity"/>

<type name="Calculator" type="IoCContainers.Components.Functions.IFunctionState, IoCContainersUnity" mapTo="IoCContainers.Components.Functions.CalculatorFunctionState, IoCContainersUnity"/>

<type type="IoCContainers.Components.Services.IConsoleInputService, IoCContainersUnity" mapTo="IoCContainers.Components.Services.ConsoleInputServiceImpl, IoCContainersUnity"> <lifetime type="singleton" /> <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,Microsoft.Practices.Unity.Configuration">

You won’t escape that easy though, you still need to tell the bootstraper to configure the container like this

IUnityContainer container = new UnityContainer;

UnityConfigurationSection section = (UnityConfigurationSection) ConfigurationManager.GetSection( "unity" ); section.Containers.Default.Configure( container );

(http://11011.net/software/vspaste)Hope this is usefull. This is the final IoC Container I wanted to explore with you reader, in the next article I’ll try to lay out some general feelings about this containers.

The code can be donwloaded 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.