RemoveService() public method

public RemoveService ( Type serviceType ) : void
serviceType System.Type
return void
Example #1
0
		[Test] // RemoveService (Type, Boolean)
		public void RemoveService2_ServiceType_Null ()
		{
			ServiceContainer sc = new ServiceContainer ();

			try {
				sc.RemoveService ((Type) null, false);
				Assert.Fail ("#A1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.AreEqual ("serviceType", ex.ParamName, "#A5");
			}

			try {
				sc.RemoveService ((Type) null, true);
				Assert.Fail ("#B1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
				Assert.IsNotNull (ex.Message, "#B4");
				Assert.AreEqual ("serviceType", ex.ParamName, "#B5");
			}
		}
Example #2
0
		[Test] // RemoveService (Type, Boolean)
		public void RemoveService2_Disposed ()
		{
			ServiceContainer sc;
			ServiceContainer parent;

			ArrayList serviceInstance1 = new ArrayList ();
			ArrayList serviceInstance2 = new ArrayList ();

			Type serviceType1 = typeof (IList);
			Type serviceType2 = typeof (IEnumerable);

			parent = new ServiceContainer ();
			sc = new ServiceContainer (parent);
			sc.AddService (serviceType1, serviceInstance1, true);
			sc.AddService (serviceType2, serviceInstance2, false);

			sc.Dispose ();

			sc.RemoveService (serviceType1, false);
			sc.RemoveService (serviceType2, false);

			Assert.AreSame (serviceInstance1, sc.GetService (serviceType1), "#A1");
			Assert.IsNull (sc.GetService (serviceType2), "#A2");

			sc.RemoveService (serviceType1, true);
			sc.RemoveService (serviceType2, true);

			Assert.IsNull (sc.GetService (serviceType1), "#B1");
			Assert.IsNull (sc.GetService (serviceType2), "#B2");
		}
Example #3
0
		[Test] // RemoveService (Type)
		public void RemoveService1_ServiceType_Null ()
		{
			ServiceContainer sc = new ServiceContainer ();

			try {
				sc.RemoveService ((Type) null);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("serviceType", ex.ParamName, "#5");
			}
		}
Example #4
0
		[Test] // RemoveService (Type)
		public void RemoveService1_Disposed ()
		{
			ServiceContainer sc;
			ServiceContainer parent;
			
			ArrayList serviceInstance1 = new ArrayList ();

			Type serviceType1 = typeof (IList);

			parent = null;
			sc = new ServiceContainer (parent);
			sc.AddService (serviceType1, serviceInstance1);
			sc.Dispose ();

			sc.RemoveService (typeof (DateTime));
		}
Example #5
0
		[Test] // RemoveService (Type)
		public void RemoveService1 ()
		{
			ServiceContainer sc;
			ServiceContainer parent;
			
			ArrayList serviceInstance1 = new ArrayList ();
			ArrayList serviceInstance2 = new ArrayList ();

			Type serviceType1 = typeof (IList);
			Type serviceType2 = typeof (IEnumerable);

			parent = null;
			sc = new ServiceContainer (parent);
			sc.AddService (serviceType1, serviceInstance1);
			sc.AddService (serviceType2, serviceInstance2);

			sc.RemoveService (typeof (DateTime));

			Assert.AreSame (serviceInstance1, sc.GetService (serviceType1), "#A1");
			Assert.AreSame (serviceInstance2, sc.GetService (serviceType2), "#A2");

			sc.RemoveService (serviceType2);

			Assert.AreSame (serviceInstance1, sc.GetService (serviceType1), "#B1");
			Assert.IsNull (sc.GetService (serviceType2), "#B2");

			sc.RemoveService (serviceType1);

			Assert.IsNull (sc.GetService (serviceType1), "#C1");
			Assert.IsNull (sc.GetService (serviceType2), "#C2");

			sc.AddService (serviceType1, serviceInstance1);
			sc.AddService (serviceType2, serviceInstance2);

			Assert.AreSame (serviceInstance1, sc.GetService (serviceType1), "#D1");
			Assert.AreSame (serviceInstance2, sc.GetService (serviceType2), "#D2");

			parent = new ServiceContainer ();
			sc = new ServiceContainer (parent);
			sc.AddService (serviceType1, serviceInstance1, true);
			sc.AddService (serviceType2, serviceInstance2, false);

			sc.RemoveService (serviceType1);

			Assert.AreSame (serviceInstance1, sc.GetService (serviceType1), "#E1");
			Assert.AreSame (serviceInstance2, sc.GetService (serviceType2), "#E2");

			sc.RemoveService (serviceType2);

			Assert.AreSame (serviceInstance1, sc.GetService (serviceType1), "#F1");
			Assert.IsNull (sc.GetService (serviceType2), "#F2");

			parent.RemoveService (serviceType1);

			Assert.IsNull (sc.GetService (serviceType1), "#G1");
			Assert.IsNull (sc.GetService (serviceType2), "#G2");

			parent = new ServiceContainer ();
			sc = new ServiceContainer (parent);
			sc.AddService (serviceType1, serviceInstance1, true);
			sc.AddService (serviceType1, serviceInstance2, false);

			sc.RemoveService (serviceType1);

			Assert.AreSame (serviceInstance1, sc.GetService (serviceType1), "#H1");
			Assert.AreSame (serviceInstance1, parent.GetService (serviceType1), "#H2");

			sc.RemoveService (serviceType1);

			Assert.AreSame (serviceInstance1, sc.GetService (serviceType1), "#I1");
			Assert.AreSame (serviceInstance1, parent.GetService (serviceType1), "#I2");

			parent.RemoveService (serviceType1);

			Assert.IsNull (sc.GetService (serviceType1), "#J1");
			Assert.IsNull (parent.GetService (serviceType1), "#J2");

			parent = new ServiceContainer ();
			sc = new ServiceContainer (parent);
			sc.AddService (serviceType1, serviceInstance1, true);
			sc.AddService (serviceType1, serviceInstance2, false);

			parent.RemoveService (serviceType1);

			Assert.AreSame (serviceInstance2, sc.GetService (serviceType1), "#K1");
			Assert.IsNull (parent.GetService (serviceType1), "#K2");
		}
        public void NestedContainers() {
            Class1 class1 = new Class1();
            Class2 class2 = new Class2();
            Class3 class3 = new Class3();
            Class1 class1a = new Class1();

            ServiceContainer parentContainer = new ServiceContainer();
            parentContainer.AddService(typeof(IInterface1), class1);

            ServiceContainer childContainer = new ServiceContainer(parentContainer);
            childContainer.AddService(typeof(IInterface2), class2);

            // child container returns what it contains and what its parent contains
            Assert.AreSame(class1, childContainer.GetService(typeof(IInterface1)));
            Assert.AreSame(class2, childContainer.GetService(typeof(IInterface2)));
            Assert.IsNull(childContainer.GetService(typeof(IInterface3)));

            // parent container only returns what it contains
            Assert.IsNull(parentContainer.GetService(typeof(IInterface2)));

            // add a service to the parent, and it becomes available to the child
            parentContainer.AddService(typeof(IInterface3), class3);
            Assert.AreSame(class3, childContainer.GetService(typeof(IInterface3)));
            Assert.AreSame(class3, parentContainer.GetService(typeof(IInterface3)));

            // remove a service from the parent, and it is no longer available to the child
            parentContainer.RemoveService(typeof(IInterface3));
            Assert.IsNull(childContainer.GetService(typeof(IInterface3)));
            Assert.IsNull(parentContainer.GetService(typeof(IInterface3)));

            // remove a service from the child container but not the parent container
            childContainer.RemoveService(typeof(IInterface1));
            Assert.AreSame(class1, childContainer.GetService(typeof(IInterface1)));
            Assert.AreSame(class1, parentContainer.GetService(typeof(IInterface1)));

            // add an implementation to the child container and it overrides the parent container
            childContainer.AddService(typeof(IInterface1), class1a);
            Assert.AreSame(class1a, childContainer.GetService(typeof(IInterface1)));
            Assert.AreSame(class1, parentContainer.GetService(typeof(IInterface1)));

            // remove implementation from the child container, and the implementation in the parent
            // container is used again in the child container
            childContainer.RemoveService(typeof(IInterface1));
            Assert.AreSame(class1, childContainer.GetService(typeof(IInterface1)));
            Assert.AreSame(class1, parentContainer.GetService(typeof(IInterface1)));

            // remove implementation in the child container using promotion 
            childContainer.RemoveService(typeof(IInterface1), true);
            Assert.IsNull(childContainer.GetService(typeof(IInterface1)));
            Assert.IsNull(parentContainer.GetService(typeof(IInterface1)));

            // add implementation to both parent and child container, and then remove from child using
            // promotion, the implementation left in the child will remain
            parentContainer.AddService(typeof(IInterface1), class1);
            childContainer.AddService(typeof(IInterface1), class1a);
            childContainer.RemoveService(typeof(IInterface1), true);
            Assert.AreSame(class1a, childContainer.GetService(typeof(IInterface1)));
            Assert.IsNull(parentContainer.GetService(typeof(IInterface1)));

            // remove using promotion again and the implementation remains in the child
            childContainer.RemoveService(typeof(IInterface1), true);
            Assert.AreSame(class1a, childContainer.GetService(typeof(IInterface1)));
            Assert.AreSame(class1a, childContainer.GetService(typeof(IInterface1)));
            Assert.IsNull(parentContainer.GetService(typeof(IInterface1)));

            // remove without promotion and the instance is gone in the child
            childContainer.RemoveService(typeof(IInterface1), false);
            Assert.IsNull(childContainer.GetService(typeof(IInterface1)));
            Assert.IsNull(parentContainer.GetService(typeof(IInterface1)));
        }