Ejemplo n.º 1
0
        internal void TypeElementUsesAlias()
        {
            UnityConfigurationSection section = ConfigurationManager.GetSection("unity") as UnityConfigurationSection;

            Assert.AreEqual(2, section.TypeAliases.Count);

            UnityTypeAlias typeAlias      = section.TypeAliases[0];
            UnityTypeAlias mapToTypeAlias = section.TypeAliases[1];

            Assert.AreEqual("ILogger", typeAlias.Alias);
            Assert.AreSame(typeof(ILogger), typeAlias.Type);

            UnityContainerElement ce = section.Containers[0];

            Assert.AreEqual(3, ce.Types.Count);

            // Check Type
            UnityTypeElement te = section.Containers[0].Types[1];

            Assert.AreSame(typeAlias.Type, te.Type);

            // Check MapTo
            UnityTypeElement te2 = section.Containers[0].Types[2];

            Assert.AreSame(mapToTypeAlias.Type, te2.MapTo);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Resolves the specified container name.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="containerName">Name of the container.</param>
        /// <returns></returns>
        static public T Resolve <T>(string containerName)
        {
            T results = default(T);
            UnityContainerElement container = UnityHelper.GetContainer(containerName);
            Type theTypeImInterestedIn      = typeof(T);

            UnityTypeElement matchedType = null;
            Boolean          bMatchFound = false;

            foreach (UnityTypeElement typeElement in container.Types)
            {
                string[] typeParts = typeElement.TypeName.Split(',');

                string typeNameDef = string.Format("{0},{1}", typeParts[0], typeParts[1]);
                string typeMapDef  = string.Format("{0}.{1},{2}", theTypeImInterestedIn.Namespace, theTypeImInterestedIn.Name, theTypeImInterestedIn.Assembly.GetName().Name);

                typeNameDef = typeNameDef.Replace(" ", string.Empty);
                typeMapDef  = typeMapDef.Replace(" ", string.Empty);

                if (string.Compare(typeNameDef, typeMapDef, true) == 0)
                {
                    bMatchFound = true;
                    matchedType = typeElement;
                    break;
                }
            }

            if (bMatchFound)
            {
                string[] typeParts = matchedType.MapToName.Split(',');
                results = ClassFactory <T> .Create(typeParts[1], typeParts[0]);
            }

            return(results);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Configures the specified IUnityContainer from the specified config section.
        /// The container config elements are processed and merged in the following order:
        /// 1. Default container;
        /// 2. env:envname container;
        /// 3. host:hostname container;
        /// 4. post-config container.
        /// </summary>
        public static IUnityContainer AddConfiguration(this IUnityContainer container, string sectionName)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            if (string.IsNullOrEmpty(sectionName))
            {
                throw new ArgumentException("The section name must be specified.", "sectionName");
            }

            UnityConfigurationSection section;

            try
            {
                section = (UnityConfigurationSection)ConfigurationManager.GetSection(sectionName);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to get the '" + sectionName + "' configuration section."
                                               + " This may happen if you have more than one IIS virtual directory configured"
                                               + " for the same physical directory.", ex);
            }

            if (section == null)
            {
                throw new InvalidOperationException(string.Format("The {0} configuration section is not found in the application configuration file.", sectionName));
            }

            UnityContainerElement defaultConfig = section.Containers.Default;

            if (defaultConfig != null)
            {
                defaultConfig.Configure(container);
            }

            UnityContainerElement environmentConfig = section.Containers["env:" + RuntimeEnvironment.EnvironmentName];

            if (environmentConfig != null)
            {
                environmentConfig.Configure(container);
            }

            UnityContainerElement hostConfig = section.Containers["host:" + RuntimeEnvironment.HostName];

            if (hostConfig != null)
            {
                hostConfig.Configure(container);
            }

            UnityContainerElement postConfig = section.Containers["post-config"];

            if (postConfig != null)
            {
                postConfig.Configure(container);
            }

            return(container);
        }
Ejemplo n.º 4
0
        public void CanSetLifetimeToSingleton()
        {
            UnityConfigurationSection section = GetUnitySection("UnnamedContainers");
            UnityContainerElement     ce      = section.Containers.Default;
            UnityTypeElement          te      = ce.Types[0];

            Assert.AreEqual(typeof(ContainerControlledLifetimeManager), te.Lifetime.Type);
        }
Ejemplo n.º 5
0
        private void TestForExpectedActions(string configName, ConfigurationActionRecord[] expectedActions)
        {
            UnityContainerElement element   = GetUnitySection(configName).Containers.Default;
            MockUnityContainer    container = new MockUnityContainer();

            element.Configure(container);

            CollectionAssert.AreEqual(expectedActions, container.ConfigActions);
        }
Ejemplo n.º 6
0
        internal void CanAccessTypeConfig()
        {
            UnityConfigurationSection section = ConfigurationManager.GetSection("unity") as UnityConfigurationSection;
            UnityContainerElement     ce      = section.Containers[0];

            UnityContainerTypeConfigurationElement te = section.Containers[0].Types[0].TypeConfig["Microsoft.Practices.Unity.TestSupport.TypeConfigMock, Tests.Unity.Configuration"];

            Type extensionType = Type.GetType(te.ExtensionType, true);

            Assert.AreSame(typeof(TypeConfigMock), extensionType);
        }
Ejemplo n.º 7
0
        internal void ContainerCanSpecifyInformationAboutTypes()
        {
            UnityConfigurationSection section = ConfigurationManager.GetSection("unity") as UnityConfigurationSection;
            UnityContainerElement     ce      = section.Containers[0];

            UnityTypeElement te = section.Containers[0].Types[2];

            Assert.AreSame(typeof(ILogger), te.Type);
            Assert.AreEqual("mapToAlias", te.Name);
            Assert.AreSame(typeof(MockLogger), te.MapTo);
        }
Ejemplo n.º 8
0
        public void ConfigFileCanSpecifyContainerWithoutAName()
        {
            UnityConfigurationSection section = GetUnitySection("UnnamedContainers");
            UnityContainerElement     ce      = section.Containers.Default;
            UnityContainerElement     ce2     = section.Containers["one"];

            Assert.IsNotNull(ce);
            Assert.IsNotNull(ce2);

            Assert.AreEqual(1, ce.Types.Count);
        }
Ejemplo n.º 9
0
        internal static UnityContainerElement GetContainer(string containerName)
        {
            if (_containers.ContainsKey(containerName))
            {
                return(_containers[containerName]);
            }

            UnityContainerElement unityContainerElement = null;

            UnityHelper.InitializeContainer();
            lock (UnityHelper._resolvedContainers)
            {
                IDictionary <string, UnityContainerElement> resolvedContainers = UnityHelper._resolvedContainers;
                if (!resolvedContainers.ContainsKey(containerName))
                {
                    Boolean bContainerFound = false;
                    UnityConfigurationSection unityConfigurationSection = null;
                    foreach (UnityConfigurationSection section in UnityHelper._unityConfigurationSections.Values)
                    {
                        if (section.Containers[containerName] != null)
                        {
                            bContainerFound           = true;
                            unityConfigurationSection = section;
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    //IUnityContainer unityContainer = null;
                    if (bContainerFound)
                    {
                        unityContainerElement = unityConfigurationSection.Containers[containerName];
                        resolvedContainers.Add(containerName, unityContainerElement);
                    }
                    else
                    {
                        //unityContainer = UnityHelper._unityContainers[containerName];
                        unityContainerElement = resolvedContainers[containerName];
                    }
                }
                else
                {
                    unityContainerElement = resolvedContainers[containerName];
                }
            }

            _containers.Add(containerName, unityContainerElement);

            return(unityContainerElement);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Resolves the container.
        /// </summary>
        /// <param name="containerName">Name of the container.</param>
        static public void ResolveContainer(string containerName)
        {
            UnityHelper.InitializeContainer();
            lock (UnityHelper._resolvedContainers)
            {
                IDictionary <string, UnityContainerElement> resolvedContainers = UnityHelper._resolvedContainers;
                if (!resolvedContainers.ContainsKey(containerName))
                {
                    Boolean bContainerFound = false;
                    UnityConfigurationSection unityConfigurationSection = null;
                    foreach (UnityConfigurationSection section in UnityHelper._unityConfigurationSections.Values)
                    {
                        if (section.Containers[containerName] != null)
                        {
                            bContainerFound           = true;
                            unityConfigurationSection = section;
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    UnityContainerElement unityContainerElement = null;
                    IUnityContainer       unityContainer        = null;
                    if (bContainerFound)
                    {
                        unityContainerElement = unityConfigurationSection.Containers[containerName];
                        unityContainer        = new UnityContainer();
                        unityContainerElement.Configure(unityContainer);
                        UnityHelper._unityContainers.Add(containerName, unityContainer);
                        resolvedContainers.Add(containerName, unityContainerElement);
                    }
                    else
                    {
                        unityContainer        = UnityHelper._unityContainers[containerName];
                        unityContainerElement = resolvedContainers[containerName];
                        unityContainerElement.Configure(unityContainer);
                    }
                }
            }
        }
Ejemplo n.º 11
0
        private static void Run(string[] args)
        {
            const string method = "Run";

            EventSource.Raise(Event.Trace, method, "Initialising TaskRunner.");

            // Grab the task group.

            if (args.Length == 0)
            {
                throw new ApplicationException("Incorrect syntax, a task group needs to be supplied.");
            }

            var taskGroup = args[0];
            var taskArgs  = new string[args.Length - 1];

            Array.Copy(args, 1, taskArgs, 0, taskArgs.Length);

            EventSource.Raise(Event.Trace, method, "Running task group '" + taskGroup + "'.");

            // Configure TaskRunner tasks.

            var taskSection = (UnityConfigurationSection)ConfigurationManager.GetSection("linkme.tasks.container");
            UnityContainerElement taskConfig = GetContainerCaseInsensitive(taskSection.Containers, taskGroup);

            if (taskConfig == null)
            {
                throw new ApplicationException(string.Format("The task group container specified on the command line, '{0}', does not exist.", taskGroup));
            }

            taskConfig.Configure(Container.Current);

            // Run the tasks.

            IEnumerable <ITask> tasks = Container.Current.ResolveAll <ITask>();

            foreach (ITask task in tasks)
            {
                task.Execute(taskArgs);
            }
        }