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

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 codeplex.

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

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<TInstance>.

The container also supports xml configuration.

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

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
    </configSections>
    
    <unity>
        <typeAliases>
            <!-- Lifetime manager types -->
            <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
        </typeAliases>
        <containers>
            <container>
                <types>
                    <type
                        type="IoCContainers.Components.Services.IWriteString, IoCContainersUnity"
                        mapTo="IoCContainers.Components.Services.ConsoleWriteString, IoCContainersUnity">
                        <lifetime type="singleton" />
                        
                    </type>
                    <type
                        type="IoCContainers.Components.Services.IClearScreen, IoCContainersUnity"
                        mapTo="IoCContainers.Components.Services.ConsoleClearScreen, IoCContainersUnity">
                        <lifetime type="singleton" />

                    </type>
                    
                    <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">
                            <constructor>
                                <param name="functionStates" parameterType="IoCContainers.Components.Functions.IFunctionState[], IoCContainersUnity">
                                    <array>
                                        <dependency name="Alarm"/>
                                        <dependency name="NewLine"/>
                                        <dependency name="Calculator"/>
                                    </array>
                                </param>
                            </constructor>
                        </typeConfig>
                        </type>
                </types>
            </container>
        </containers>                
    </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 );

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 codeplex.

Posted on Thursday, August 20, 2009 9:35 AM IoC | Back to top


Comments on this post: IoC Containers in .NET part 6: Unity Container

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Sharpoverride | Powered by: GeeksWithBlogs.net