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]