UnityManager.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » Library » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Kooboo 
Kooboo » Everest » Library » UnityManager.cs
/*
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);
        }
    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.