Beispiel #1
0
        public void TestCollections()
        {
            IMutablePicoContainer mpc = new DefaultPicoContainer();

            IParameter[] parameters = new IParameter[]
            {
                new ComponentParameter(typeof(Cod), false),
                new ComponentParameter(typeof(Fish), false)
            };

            mpc.RegisterComponentImplementation(typeof(CollectedBowl), typeof(CollectedBowl), parameters);
            mpc.RegisterComponentImplementation(typeof(Cod));
            mpc.RegisterComponentImplementation(typeof(Shark));
            Cod           cod  = (Cod)mpc.GetComponentInstanceOfType(typeof(Cod));
            CollectedBowl bowl = (CollectedBowl)mpc.GetComponentInstance(typeof(CollectedBowl));

            Assert.AreEqual(1, bowl.cods.Length);
            Assert.AreEqual(2, bowl.fishes.Length);
            Assert.AreSame(cod, bowl.cods[0]);

            try
            {
                Assert.AreSame(bowl.fishes[0], bowl.fishes[1]);
                Assert.Fail("The fishes should not be the same");
            }
            catch (AssertionException)
            {
            }
        }
		public void ParentComponentRegisteredAsClassShouldBePreffered()
		{
			DefaultPicoContainer parent = new DefaultPicoContainer();
			DefaultPicoContainer child = new DefaultPicoContainer(parent);

			parent.RegisterComponentImplementation(typeof (ITouchable), typeof (AlternativeTouchable));
			child.RegisterComponentImplementation("key", typeof (SimpleTouchable));
			child.RegisterComponentImplementation(typeof (DependsOnTouchable));

			DependsOnTouchable dot = (DependsOnTouchable) child.GetComponentInstanceOfType(typeof (DependsOnTouchable));
			Assert.AreEqual(typeof (AlternativeTouchable), dot.getTouchable().GetType());
		}
Beispiel #3
0
        public void ParentComponentRegisteredAsClassShouldBePreffered()
        {
            DefaultPicoContainer parent = new DefaultPicoContainer();
            DefaultPicoContainer child  = new DefaultPicoContainer(parent);

            parent.RegisterComponentImplementation(typeof(ITouchable), typeof(AlternativeTouchable));
            child.RegisterComponentImplementation("key", typeof(SimpleTouchable));
            child.RegisterComponentImplementation(typeof(DependsOnTouchable));

            DependsOnTouchable dot = (DependsOnTouchable)child.GetComponentInstanceOfType(typeof(DependsOnTouchable));

            Assert.AreEqual(typeof(AlternativeTouchable), dot.getTouchable().GetType());
        }
		public void ComponensRegisteredWithClassKeyTakePrecedenceOverOthersWhenThereAreMultipleImplementations()
		{
			DefaultPicoContainer pico = new DefaultPicoContainer();
			pico.RegisterComponentImplementation("default", typeof (SimpleTouchable));

			/*
			 * By using a class as key, this should take precedence over the other Touchable (Simple)
			 */
			pico.RegisterComponentImplementation(typeof (ITouchable),
			                                     typeof (DecoratedTouchable),
			                                     new IParameter[] {new ComponentParameter("default")});

			ITouchable touchable = (ITouchable) pico.GetComponentInstanceOfType(typeof (ITouchable));
			Assert.AreEqual(typeof (DecoratedTouchable), touchable.GetType());
		}
		public void testIComponentAdapterResolutionIsFirstLookedForByClassKeyToTheTopOfTheContainerHierarchy()
		{
			DefaultPicoContainer pico = new DefaultPicoContainer();
			pico.RegisterComponentImplementation("default", typeof (SimpleTouchable));

			pico.RegisterComponentImplementation(typeof (ITouchable), typeof (DecoratedTouchable), new IParameter[]
				{
					new ComponentParameter("default")
				});

			DefaultPicoContainer grandChild = new DefaultPicoContainer(new DefaultPicoContainer(new DefaultPicoContainer(pico)));

			ITouchable touchable = (ITouchable) grandChild.GetComponentInstanceOfType(typeof (ITouchable));
			Assert.AreEqual(typeof (DecoratedTouchable), touchable.GetType());

		}
Beispiel #6
0
        public void ComponensRegisteredWithClassKeyTakePrecedenceOverOthersWhenThereAreMultipleImplementations()
        {
            DefaultPicoContainer pico = new DefaultPicoContainer();

            pico.RegisterComponentImplementation("default", typeof(SimpleTouchable));

            /*
             * By using a class as key, this should take precedence over the other Touchable (Simple)
             */
            pico.RegisterComponentImplementation(typeof(ITouchable),
                                                 typeof(DecoratedTouchable),
                                                 new IParameter[] { new ComponentParameter("default") });

            ITouchable touchable = (ITouchable)pico.GetComponentInstanceOfType(typeof(ITouchable));

            Assert.AreEqual(typeof(DecoratedTouchable), touchable.GetType());
        }
Beispiel #7
0
        public void testIComponentAdapterResolutionIsFirstLookedForByClassKeyToTheTopOfTheContainerHierarchy()
        {
            DefaultPicoContainer pico = new DefaultPicoContainer();

            pico.RegisterComponentImplementation("default", typeof(SimpleTouchable));

            pico.RegisterComponentImplementation(typeof(ITouchable), typeof(DecoratedTouchable), new IParameter[]
            {
                new ComponentParameter
                    ("default")
            });

            DefaultPicoContainer grandChild =
                new DefaultPicoContainer(new DefaultPicoContainer(new DefaultPicoContainer(pico)));

            ITouchable touchable = (ITouchable)grandChild.GetComponentInstanceOfType(typeof(ITouchable));

            Assert.AreEqual(typeof(DecoratedTouchable), touchable.GetType());
        }
		public void TestCollections()
		{
			IMutablePicoContainer mpc = new DefaultPicoContainer();
			IParameter[] parameters = new IParameter[]{new ComponentParameter(typeof(Cod), false), 
														  new ComponentParameter(typeof(Fish), false)};

			mpc.RegisterComponentImplementation(typeof(CollectedBowl), typeof(CollectedBowl), parameters);
			mpc.RegisterComponentImplementation(typeof(Cod));
			mpc.RegisterComponentImplementation(typeof(Shark));
			Cod cod = (Cod) mpc.GetComponentInstanceOfType(typeof(Cod));
			CollectedBowl bowl = (CollectedBowl) mpc.GetComponentInstance(typeof(CollectedBowl));
			Assert.AreEqual(1, bowl.cods.Length);
			Assert.AreEqual(2, bowl.fishes.Length);
			Assert.AreSame(cod, bowl.cods[0]);

			try
			{
				Assert.AreSame(bowl.fishes[0], bowl.fishes[1]);
				Assert.Fail("The fishes should not be the same");
			}
			catch(AssertionException) {}
		}