示例#1
0
        public void RegisteredComponentsExistAndAreTheCorrectTypes()
        {
            IPicoContainer pico = CreatePicoContainerWithTouchableAndDependsOnTouchable();

            Assert.IsNotNull(pico.GetComponentAdapter(typeof(ITouchable)));
            Assert.IsNotNull(pico.GetComponentAdapter(typeof(DependsOnTouchable)));
            Assert.IsTrue(pico.GetComponentInstance(typeof(ITouchable)) is ITouchable);
            Assert.IsTrue(pico.GetComponentInstance(typeof(DependsOnTouchable)) is DependsOnTouchable);
            Assert.IsNull(pico.GetComponentAdapter(typeof(ICollection)));
        }
 public virtual object ResolveInstance(IPicoContainer container, IComponentAdapter adapter, Type expectedType)
 {
     IComponentAdapter componentAdapter = ResolveAdapter(container, adapter, expectedType);
     if (componentAdapter != null)
     {
         return container.GetComponentInstance(componentAdapter.ComponentKey);
     }
     return null;
 }
示例#3
0
        public virtual object ResolveInstance(IPicoContainer container, IComponentAdapter adapter, Type expectedType)
        {
            IComponentAdapter componentAdapter = ResolveAdapter(container, adapter, expectedType);

            if (componentAdapter != null)
            {
                return(container.GetComponentInstance(componentAdapter.ComponentKey));
            }
            return(null);
        }
        private static object[] GetArrayInstance(IPicoContainer container, Type expectedType, IDictionary adapterList)
        {
            object[] result = (object[])Array.CreateInstance(expectedType.GetElementType(), adapterList.Count);
            int      i      = 0;

            foreach (DictionaryEntry entry in adapterList)
            {
                IComponentAdapter componentAdapter = (IComponentAdapter)entry.Value;
                result[i] = container.GetComponentInstance(componentAdapter.ComponentKey);
                i++;
            }
            return(result);
        }
示例#5
0
        private object GetInstance(IComponentAdapter componentAdapter)
        {
            // check wether this is our adapter
            // we need to check this to ensure up-down dependencies cannot be followed
            bool isLocal = componentAdapters.Contains(componentAdapter);

            if (isLocal)
            {
                Object instance = componentAdapter.GetComponentInstance(this);
                AddOrderedComponentAdapter(componentAdapter);

                return(instance);
            }
            else if (parent != null)
            {
                return(parent.GetComponentInstance(componentAdapter.ComponentKey));
            }

            // TODO: decide .. exception or null?
            // exceptrion: mx: +1, joehni +1
            return(null);
        }
示例#6
0
        public void GettingComponentWithMissingDependencyFails()
        {
            IPicoContainer picoContainer = CreatePicoContainerWithDependsOnTouchableOnly();

            try
            {
                picoContainer.GetComponentInstance(typeof(DependsOnTouchable));
                Assert.Fail("should need a Touchable");
            }
            catch (UnsatisfiableDependenciesException e)
            {
                Assert.AreSame(
                    picoContainer.GetComponentAdapterOfType(typeof(DependsOnTouchable)).ComponentImplementation,
                    e.UnsatisfiableComponentAdapter.ComponentImplementation);
                IList unsatisfiableDependencies = e.UnsatisfiableDependencies;
                Assert.AreEqual(1, unsatisfiableDependencies.Count);
                Assert.AreEqual(typeof(ITouchable), unsatisfiableDependencies[0]);
            }
        }
        private static ICollection GetCollectionInstance(IPicoContainer container, Type expectedType, IDictionary adapterList)
        {
            Type collectionType = expectedType;

            if (collectionType.IsInterface)
            {
                // The order of tests are significant. The least generic types last.
                if (typeof(IList).IsAssignableFrom(collectionType))
                {
                    collectionType = typeof(ArrayList);
                }
//            } else if (BlockingQueue.class.isAssignableFrom(collectionType)) {
//                collectionType = ArrayBlockingQueue.class;
//            } else if (Queue.class.isAssignableFrom(collectionType)) {
//                collectionType = LinkedList.class;

                /*} else if (SortedSet.class.isAssignableFrom(collectionType)) {
                 * collectionType = TreeSet.class;
                 * } else if (Set.class.isAssignableFrom(collectionType)) {
                 * collectionType = HashSet.class;
                 * }*/
                else if (typeof(ICollection).IsAssignableFrom(collectionType))
                {
                    collectionType = typeof(ArrayList);
                }
            }
            try
            {
                IList result = (IList)Activator.CreateInstance(collectionType);

                foreach (DictionaryEntry entry in adapterList)
                {
                    IComponentAdapter componentAdapter = (IComponentAdapter)entry.Value;
                    result.Add(container.GetComponentInstance(componentAdapter.ComponentKey));
                }

                return(result);
            }
            catch (Exception e)
            {
                throw new PicoInitializationException(e);
            }
        }
        private static IDictionary GetDictionaryInstance(IPicoContainer container, Type expectedType, IDictionary adapterList)
        {
            Type collectionType = expectedType;

            if (collectionType.IsInterface)
            {
                collectionType = typeof(Hashtable);
            }

            try
            {
                IDictionary result = (IDictionary)Activator.CreateInstance(collectionType);
                foreach (DictionaryEntry entry in adapterList)
                {
                    Object key = entry.Key;
                    result.Add(key, container.GetComponentInstance(key));
                }
                return(result);
            }
            catch (Exception e)
            {
                throw new PicoInitializationException(e);
            }
        }
		private IDictionary GetDictionaryInstance(IPicoContainer container, Type expectedType, IDictionary adapterList) 
		{
			Type collectionType = expectedType;
			if (collectionType.IsInterface) 
			{
				collectionType = typeof(Hashtable);
			}

			try 
			{
				IDictionary result = (IDictionary) Activator.CreateInstance(collectionType);
				foreach (DictionaryEntry entry in adapterList)
				{
					Object key = entry.Key;
					result.Add(key, container.GetComponentInstance(key));
				}
				return result;
			} 
			catch (Exception e)
			{
				throw new PicoInitializationException(e);
			}
		}
		private ICollection GetCollectionInstance(IPicoContainer container, Type expectedType, IDictionary adapterList)
		{
			Type collectionType = expectedType;
			if (collectionType.IsInterface)
			{
				// The order of tests are significant. The least generic types last.
				if (typeof (IList).IsAssignableFrom(collectionType))
				{
					collectionType = typeof (ArrayList);
				}
//            } else if (BlockingQueue.class.isAssignableFrom(collectionType)) {
//                collectionType = ArrayBlockingQueue.class;
//            } else if (Queue.class.isAssignableFrom(collectionType)) {
//                collectionType = LinkedList.class;
					/*} else if (SortedSet.class.isAssignableFrom(collectionType)) {
                collectionType = TreeSet.class;
            } else if (Set.class.isAssignableFrom(collectionType)) {
                collectionType = HashSet.class;
            }*/
				else if (typeof (ICollection).IsAssignableFrom(collectionType))
				{
					collectionType = typeof (ArrayList);
				}
			}
			try
			{
				IList result = (IList) Activator.CreateInstance(collectionType);

				foreach (DictionaryEntry entry in adapterList)
				{
					IComponentAdapter componentAdapter = (IComponentAdapter) entry.Value;
					result.Add(container.GetComponentInstance(componentAdapter.ComponentKey));
				}

				return result;
			}
			catch (Exception e)
			{
				throw new PicoInitializationException(e);

			}
		}
		private object[] GetArrayInstance(IPicoContainer container, Type expectedType, IDictionary adapterList)
		{
			object[] result = (object[]) Array.CreateInstance(expectedType.GetElementType(), adapterList.Count);
			int i = 0;

			foreach (DictionaryEntry entry in adapterList)
			{
				IComponentAdapter componentAdapter = (IComponentAdapter) entry.Value;
				result[i] = container.GetComponentInstance(componentAdapter.ComponentKey);
				i++;
			}
			return result;
		}