コード例 #1
0
        public void using_onRemove_action_reentrancy_of_remove_is_handled()
        {
            SimpleServiceContainer container = new SimpleServiceContainer();
            DisposableThatReenterClearWhenDisposed disposableClass = new DisposableThatReenterClearWhenDisposed();
            EmptyClass stupidObject = new EmptyClass();

            disposableClass.ServiceContainer = container;
            container.Add(disposableClass, Util.ActionDispose);
            container.Add(stupidObject, e => container.Clear());

            DisposableThatReenterClearWhenDisposed service = container.GetService <DisposableThatReenterClearWhenDisposed>();

            Assert.That(service, Is.InstanceOf <DisposableThatReenterClearWhenDisposed>());

            container.Remove <EmptyClass>();

            Assert.That(container.GetService(typeof(DisposableThatReenterClearWhenDisposed)), Is.Null);
            Assert.That(container.GetService <DisposableThatReenterClearWhenDisposed>(), Is.Null);
            Assert.Throws <CKException>(() => container.GetService <DisposableThatReenterClearWhenDisposed>(true));
        }
コード例 #2
0
        public void removing_a_registered_service()
        {
            int removedServicesCount = 0;

            SimpleServiceContainer container = new SimpleServiceContainer();

            container.Add(typeof(IAddService), new AddServiceImpl(), o => removedServicesCount++);

            IAddService service = container.GetService <IAddService>();

            Assert.That(service, Is.InstanceOf <AddServiceImpl>());

            Assert.That(removedServicesCount, Is.EqualTo(0));
            container.Remove(typeof(IAddService));
            Assert.That(removedServicesCount, Is.EqualTo(1));

            Assert.That(container.GetService(typeof(IAddService)), Is.Null);
            Assert.That(container.GetService <IAddService>(), Is.Null);
            Assert.Throws <CKException>(() => container.GetService <IAddService>(true));
        }
コード例 #3
0
        public void removing_a_registered_service()
        {
            int removedServicesCount = 0;

            SimpleServiceContainer container = new SimpleServiceContainer();

            container.Add(typeof(IAddService), new AddServiceImpl(), o => removedServicesCount++);

            IAddService service = container.GetService <IAddService>();

            service.Should().BeOfType <AddServiceImpl>();

            removedServicesCount.Should().Be(0);
            container.Remove(typeof(IAddService));
            removedServicesCount.Should().Be(1);

            container.GetService(typeof(IAddService)).Should().BeNull();
            container.GetService <IAddService>().Should().BeNull();
            container.Invoking(sut => sut.GetService <IAddService>(true)).Should().Throw <Exception>();
        }
コード例 #4
0
        public void using_onRemove_action_reentrancy_of_remove_is_handled()
        {
            SimpleServiceContainer container = new SimpleServiceContainer();
            DisposableThatReenterClearWhenDisposed disposableClass = new DisposableThatReenterClearWhenDisposed();
            EmptyClass stupidObject = new EmptyClass();

            disposableClass.ServiceContainer = container;
            container.Add(disposableClass, Util.ActionDispose);
            container.Add(stupidObject, e => container.Clear());

            DisposableThatReenterClearWhenDisposed service = container.GetService <DisposableThatReenterClearWhenDisposed>();

            service.Should().BeOfType <DisposableThatReenterClearWhenDisposed>();

            container.Remove <EmptyClass>();

            container.GetService(typeof(DisposableThatReenterClearWhenDisposed)).Should().BeNull();
            container.GetService <DisposableThatReenterClearWhenDisposed>().Should().BeNull();
            container.Invoking(sut => sut.GetService <DisposableThatReenterClearWhenDisposed>(true)).Should().Throw <Exception>();
        }
コード例 #5
0
        public void SimpleServiceContainerFallbackTest()
        {
            int removedServicesCount = 0;

            SimpleServiceContainer container = new SimpleServiceContainer();

            container.Add(typeof(IAddService), new AddServiceImpl(), o => removedServicesCount++);

            IAddService service = container.GetService <IAddService>();

            Assert.That(service != null);
            Assert.That(service.GetType(), Is.EqualTo(typeof(AddServiceImpl)));
            Assert.That(service.Add(1, 1), Is.EqualTo(2));

            Assert.That(removedServicesCount, Is.EqualTo(0));
            container.Remove(typeof(IAddService));
            Assert.That(removedServicesCount, Is.EqualTo(1));

            Assert.That(container.GetService(typeof(IAddService)), Is.Null);
            Assert.That(container.GetService <IAddService>(), Is.Null);
            Assert.Throws <CKException>(() => container.GetService <IAddService>(true));
        }