first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

Unity C# Code: Static Service Locator Pattern for Unity; Richard Banks; StackOverflow.com

using System.Configuration; using Microsoft.Practices.Unity; using Microsoft.Practices.Unity.Configuration;

namespace Utilities { public static class ServiceLocator { private static IUnityContainer container = new UnityContainer(); private static bool isConfigured;

    public static void Clear()
    {
            container = new UnityContainer();
    }

    private static IUnityContainer Container
    {
            get
            {
                    if (!isConfigured)
                    {
                            ConfigureContainer();
                    }
                    return container;
            }
    }

    public static T Resolve<T>()
    {
            return Container.Resolve<T>();
    }

    public static T Resolve<T>(string name)
    {
            return Container.Resolve<T>(name);
    }

    public static void AddInstance<T>(object instance)
    {
            Container.RegisterInstance(typeof (T), instance);
    }

    private static void ConfigureContainer()
    {
                    UnityConfigurationSection section = (UnityConfigurationSection) ConfigurationManager.GetSection("unity");
                    section.Containers.Default.Configure(container);
                    isConfigured = true;            
    }
}

}

[http://stackoverflow.com/questions/557657/shoud-i-make-an-iunitycontainer-object-use-a-singleton-pattern]

mod date: 2010-03-30T06:50:54.000Z