Пример #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);
        }
Пример #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);
        }
Пример #3
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);
        }
Пример #4
0
        public void EmptyStringForLifetimeTypeGivesNullLifetimeManager()
        {
            UnityConfigurationSection section     = GetUnitySection("Lifetimes");
            UnityTypeElement          typeElement = section.Containers.Default.Types[4];

            LifetimeManager lifetime = typeElement.Lifetime.CreateLifetimeManager();

            Assert.IsNull(lifetime);
        }
Пример #5
0
        public void CanUseCustomTypeConverterToSpecifyLifetime()
        {
            UnityConfigurationSection section     = GetUnitySection("Lifetimes");
            UnityTypeElement          typeElement = section.Containers.Default.Types[3];

            SessionLifetimeManager lifetime =
                (SessionLifetimeManager)typeElement.Lifetime.CreateLifetimeManager();

            Assert.AreEqual("sdrawkcab", lifetime.SessionKey);
        }
Пример #6
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);
        }