/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Everest.Library.ExtensionMethod;
using Everest.Library.Providers.Caching;
namespace Everest.Library{
/// <summary>
///
/// </summary>
public class UnityManager
{
public class UnityManagerRefresh : ICacheItemRefreshAction
{
#region ICacheItemRefreshAction Members
public void Refresh(string removedKey, object expiredValue, CacheItemRemovedReason removalReason)
{
UnityManager.ClearContainers();
}
#endregion
}
//static string UnityConfigFile = AppDomain.CurrentDomain.BaseDirectory + "\\unity.config";
//private const string CacheKey = "CacheItem-Global-UnityManager-UnityContainers";
/// <summary>
///
/// </summary>
private static object lockHelper = new object();
private static bool initialized = false;
internal const string DefaultContainerName = "Default";
private static volatile IDictionary<string, IUnityContainer> containers = null;
/// <summary>
/// Gets the Containers.
/// </summary>
/// <value>The Containers.</value>
public static IDictionary<string, IUnityContainer> Containers
{
get
{
if (initialized == false)
{
lock (lockHelper)
{
if (initialized == false)
{
containers = new Dictionary<string, IUnityContainer>();
UnityConfigurationSection unitySection = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
if (unitySection != null)
{
InitiateContainers(unitySection, containers);
}
if (!containers.ContainsKey(DefaultContainerName))
{
IUnityContainer container = new UnityContainer();
container.AddExtension(new UnityExtensions.UnityExtensionWithTypeTracking());
containers.Add(DefaultContainerName, container);
}
initialized = true;
}
}
}
return containers;
}
}
internal static void InitiateContainers(UnityConfigurationSection unitySection, IDictionary<string, IUnityContainer> containers)
{
foreach (UnityContainerElement element in unitySection.Containers)
{
string name = string.IsNullOrEmpty(element.Name) ? DefaultContainerName : element.Name;
if (!containers.ContainsKey(name))
{
IUnityContainer container = new UnityContainer();
container.AddExtension(new UnityExtensions.UnityExtensionWithTypeTracking());
containers.Add(name, container);
}
element.Configure(containers[name]);
}
}
/// <summary>
/// Clears the containers.
/// </summary>
private static void ClearContainers()
{
containers.Clear();
containers = null;
}
/// <summary>
/// Resolves the specified type's instance from default container.
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public static object Resolve(Type t)
{
return Containers[DefaultContainerName].Resolve(t);
}
/// <summary>
/// Resolves the specified type's instance from default container.
/// </summary>
/// <param name="t">The t.</param>
/// <param name="name">The name.</param>
/// <returns></returns>
public static object Resolve(Type t, string name)
{
return Containers[DefaultContainerName].Resolve(t, name);
}
/// <summary>
/// Resolves the specified type's instance from default container.
/// If someone in Containers return until find first one,otherwise return null.
/// </summary>
/// <typeparam name="T">type</typeparam>
/// <returns></returns>
public static T Resolve<T>()
{
return Containers[DefaultContainerName].Resolve<T>();
}
/// <summary>
/// Resolves all.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static IEnumerable<T> ResolveAll<T>()
{
return Containers[DefaultContainerName].ResolveAll<T>();
}
/// <summary>
/// Resolves the specified type's instance from default container.
/// If someone in Containers return until find first one,otherwise return null.
/// </summary>
/// <typeparam name="T">type</typeparam>
/// <param name="name">The name.</param>
/// <returns></returns>
public static T Resolve<T>(string name)
{
return Containers[DefaultContainerName].Resolve<T>(name);
}
/// <summary>
/// Resolves the specified type's instance from the specified container.
/// </summary>
/// <param name="containerName">Name of the container.</param>
/// <param name="type">The type.</param>
/// <returns></returns>
public static object ResolveSpecifiedContainer(string containerName, Type t)
{
if (!Containers.ContainsKey(containerName))
{
throw new ArgumentException("specified container no exist.", containerName);
}
return Containers[containerName].Resolve(t);
}
/// <summary>
/// Resolves the specified type's instance from the specified container.
/// </summary>
/// <param name="containerName">Name of the container.</param>
/// <param name="t">The t.</param>
/// <param name="name">The name.</param>
/// <returns></returns>
public static object ResolveSpecifiedContainer(string containerName, Type t, string name)
{
if (!Containers.ContainsKey(containerName))
{
throw new ArgumentException("specified container no exist.", containerName);
}
return Containers[containerName].Resolve(t, name);
}
/// <summary>
/// Resolves the specified type's instance from the specified container.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="containerName">Name of the container.</param>
/// <returns></returns>
public static T ResolveSpecifiedContainer<T>(string containerName)
{
if (!Containers.ContainsKey(containerName))
{
throw new ArgumentException("specified container no exist.", containerName);
}
return Containers[containerName].Resolve<T>();
}
/// <summary>
/// Resolves the specified type's instance from the specified container.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="containerName">Name of the container.</param>
/// <returns></returns>
public static T ResolveSpecifiedContainer<T>(string containerName, string name)
{
if (!Containers.ContainsKey(containerName))
{
throw new ArgumentException("specified container no exist.", containerName);
}
return Containers[containerName].Resolve<T>(name);
}
/// <summary>
/// Tears down.
/// </summary>
/// <param name="o">The o.</param>
/// <param name="containerName">Name of the container.</param>
public static void TearDown(object o, string containerName)
{
containerName = EnsureContainerName(containerName);
Containers[containerName].Teardown(o);
}
/// <summary>
/// Ensures the name of the container.
/// </summary>
/// <param name="containerName">Name of the container.</param>
/// <returns></returns>
private static string EnsureContainerName(string containerName)
{
containerName = string.IsNullOrEmpty(containerName) ? DefaultContainerName : containerName;
DesignByContract.Require(Containers.ContainsKey(containerName), "specified container no exist.");
return containerName;
}
/// <summary>
/// Determine the specified type mapping if existses in the unity container.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="containerName">Name of the container.</param>
/// <param name="name">The name.</param>
/// <returns></returns>
public static bool Exists<T>(string containerName, string name)
{
containerName = EnsureContainerName(containerName);
return Containers[containerName].Exists<T>(name);
}
}
}
|