示例#1
0
        public void Instance()
        {
            var container = new ServiceContainer();

            Assert.IsNull(container.GetService <ISampleSvc>(), "#A01");
            Assert.AreEqual(container, container.GetService <IServicePublisher>(), "#A08");

            var sampleSvc = new SampleSvc();

            using (container.Publish <ISampleSvc>(sampleSvc))
            {
                Assert.AreEqual(sampleSvc, container.GetService <ISampleSvc>(), "#A02");
                Assert.Throws <ArgumentException>(() => container.Publish <ISampleSvc>(sampleSvc));
            }
            Assert.IsNull(container.GetService <ISampleSvc>(), "#A03");
        }
 protected void SetUp()
 {
     _svcContainer = new ServiceContainer();
     _extMgr       = new ExtensionManager(_svcContainer);
     _extMgr.ReflectionOnlyScan(new ActivePartRegStrategy(_svcContainer), typeof(TestActivePart).Assembly.FullName);
     _svcContainer.Publish <IActivePartManager>(sp => new ActivePartManager(_svcContainer));
 }
示例#3
0
        public void ContainerChain()
        {
            var parent = new ServiceContainer();
            var child  = new ServiceContainer(parent);
            var svc    = new SampleSvc();

            using (parent.Publish <ISampleSvc>(svc))
                Assert.AreEqual(svc, child.GetService <ISampleSvc>(), "#A01");
            Assert.IsNull(child.GetService <ISampleSvc>(), "#A02");
        }
示例#4
0
        public void SvcDispose()
        {
            var calls = 0;

            using (var container = new ServiceContainer())
            {
                using (container.Publish <IDisposable>(sp => Disposable.Create(() => calls++)))
                {
                    Assert.AreEqual(0, calls, "#A01");
                    // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
                    container.GetService <IDisposable>();
                    Assert.AreEqual(0, calls, "#A02");
                }
                Assert.AreEqual(1, calls, "#A03");
                container.Publish <IDisposable>(sp => Disposable.Create(() => calls++));
                // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
                container.GetService <IDisposable>();
                Assert.AreEqual(1, calls, "#A04");
            }
            Assert.AreEqual(2, calls, "#A05");
        }
示例#5
0
        public void InstanceFactory()
        {
            var container = new ServiceContainer();
            var sampleSvc = new SampleSvc();
            var calls     = 0;

            using (
                container.Publish <ISampleSvc>(
                    // ReSharper disable ImplicitlyCapturedClosure
                    sp =>
                    // ReSharper restore ImplicitlyCapturedClosure
            {
                calls++;
                return(sampleSvc);
            }))
            {
                Assert.AreEqual(0, calls, "#A01");
                Assert.AreEqual(sampleSvc, container.GetService <ISampleSvc>(), "#A02");
                Assert.AreEqual(1, calls, "#A03");
            }
            Assert.IsNull(container.GetService <ISampleSvc>(), "#A04");
        }