예제 #1
0
        public void SameLevelDependenciesSatisfied()
        {
            IKernel child = new DefaultKernel();

            Kernel.Register(Component.For(typeof(DefaultTemplateEngine)).Named("templateengine"));
            Kernel.Register(Component.For(typeof(DefaultSpamService)).Named("spamservice"));

            Kernel.AddChildKernel(child);

            child.Register(Component.For(typeof(DefaultMailSenderService)).Named("mailsender"));

            var spamservice = child.Resolve <DefaultSpamService>("spamservice");

            Assert.IsNotNull(spamservice);
            Assert.IsNotNull(spamservice.MailSender);
            Assert.IsNotNull(spamservice.TemplateEngine);
        }
예제 #2
0
        public void DependenciesSatisfiedAmongContainers()
        {
            IKernel subkernel = new DefaultKernel();

            Kernel.Register(Component.For(typeof(DefaultMailSenderService)).Named("mailsender"));
            Kernel.Register(Component.For(typeof(DefaultTemplateEngine)).Named("templateengine"));

            Kernel.AddChildKernel(subkernel);

            subkernel.Register(Component.For(typeof(DefaultSpamService)).Named("spamservice"));

            var spamservice = subkernel.Resolve <DefaultSpamService>("spamservice");

            Assert.IsNotNull(spamservice);
            Assert.IsNotNull(spamservice.MailSender);
            Assert.IsNotNull(spamservice.TemplateEngine);
        }
예제 #3
0
        public void Parent_component_will_NOT_have_dependencies_from_child()
        {
            Kernel.Register(Component.For <DefaultTemplateEngine>(),
                            Component.For <DefaultSpamService>());

            var child = new DefaultKernel();

            Kernel.AddChildKernel(child);

            child.Register(Component.For <DefaultMailSenderService>());

            var spamservice = child.Resolve <DefaultSpamService>();

            Assert.IsNotNull(spamservice);
            Assert.IsNotNull(spamservice.TemplateEngine);
            Assert.IsNull(spamservice.MailSender);
        }
        public void RemoveChildKernelCleansUp()
        {
            IKernel subkernel      = new DefaultKernel();
            var     eventCollector = new EventsCollector(subkernel);

            subkernel.RemovedAsChildKernel += eventCollector.RemovedAsChildKernel;
            subkernel.AddedAsChildKernel   += eventCollector.AddedAsChildKernel;

            Kernel.AddChildKernel(subkernel);
            Assert.AreEqual(Kernel, subkernel.Parent);
            Assert.AreEqual(1, eventCollector.Events.Count);
            Assert.AreEqual(EventsCollector.Added, eventCollector.Events[0]);

            Kernel.RemoveChildKernel(subkernel);
            Assert.IsNull(subkernel.Parent);
            Assert.AreEqual(2, eventCollector.Events.Count);
            Assert.AreEqual(EventsCollector.Removed, eventCollector.Events[1]);
        }
        public void RemovingChildKernelUnsubscribesFromParentEvents()
        {
            IKernel subkernel      = new DefaultKernel();
            var     eventCollector = new EventsCollector(subkernel);

            subkernel.RemovedAsChildKernel += eventCollector.RemovedAsChildKernel;
            subkernel.AddedAsChildKernel   += eventCollector.AddedAsChildKernel;

            Kernel.AddChildKernel(subkernel);
            Kernel.RemoveChildKernel(subkernel);
            Kernel.AddChildKernel(subkernel);
            Kernel.RemoveChildKernel(subkernel);

            Assert.AreEqual(4, eventCollector.Events.Count);
            Assert.AreEqual(EventsCollector.Added, eventCollector.Events[0]);
            Assert.AreEqual(EventsCollector.Removed, eventCollector.Events[1]);
            Assert.AreEqual(EventsCollector.Added, eventCollector.Events[2]);
            Assert.AreEqual(EventsCollector.Removed, eventCollector.Events[3]);
        }
        public void ChildKernelOverloadsParentKernel2()
        {
            var instance1 = new DefaultTemplateEngine();
            var instance2 = new DefaultTemplateEngine();

            IKernel subkernel = new DefaultKernel();

            Kernel.AddChildKernel(subkernel);

            // subkernel added first, then populated with overloaded components after

            Kernel.Register(Component.For <DefaultTemplateEngine>().Named("engine").Instance(instance2));
            Assert.AreEqual(instance2, Kernel.Resolve <DefaultTemplateEngine>("engine"));
            Assert.AreEqual(instance2, subkernel.Resolve <DefaultTemplateEngine>("engine"));

            subkernel.Register(Component.For <DefaultTemplateEngine>().Named("engine").Instance(instance1));
            Assert.AreEqual(instance1, subkernel.Resolve <DefaultTemplateEngine>("engine"));
            Assert.AreEqual(instance2, Kernel.Resolve <DefaultTemplateEngine>("engine"));
        }
        public void ChildKernelOverloadsParentKernel1()
        {
            var instance1 = new DefaultTemplateEngine();
            var instance2 = new DefaultTemplateEngine();

            // subkernel added with already registered components that overload parent components.

            IKernel subkernel = new DefaultKernel();

            subkernel.Register(Component.For <DefaultTemplateEngine>().Named("engine").Instance(instance1));
            Assert.AreEqual(instance1, subkernel.Resolve <DefaultTemplateEngine>("engine"));

            Kernel.Register(Component.For <DefaultTemplateEngine>().Named("engine").Instance(instance2));
            Assert.AreEqual(instance2, Kernel.Resolve <DefaultTemplateEngine>("engine"));

            Kernel.AddChildKernel(subkernel);
            Assert.AreEqual(instance1, subkernel.Resolve <DefaultTemplateEngine>("engine"));
            Assert.AreEqual(instance2, Kernel.Resolve <DefaultTemplateEngine>("engine"));
        }
        public void Three_level_hierarchy([Values(0, 1, 2)] int parentComponentContainer,
                                          [Values(0, 1, 2)] int childComponentContainer)
        {
            var subKernel    = new DefaultKernel();
            var subSubKernel = new DefaultKernel();

            Kernel.AddChildKernel(subKernel);
            subKernel.AddChildKernel(subSubKernel);
            var containers = new[]
            {
                Kernel,
                subKernel,
                subSubKernel
            };

            containers[parentComponentContainer].Register(Component.For <UsesSimpleComponent1>());
            containers[childComponentContainer].Register(Component.For <SimpleComponent1>());

            subSubKernel.Resolve <UsesSimpleComponent1>();
        }
        public void Do_NOT_UseChildComponentsForParentDependenciesWhenRequestedFromChild()
        {
            IKernel subkernel = new DefaultKernel();

            Kernel.Register(Component.For(typeof(DefaultSpamService)).Named("spamservice").LifeStyle.Is(LifestyleType.Transient));
            Kernel.Register(Component.For(typeof(DefaultMailSenderService)).Named("mailsender"));
            Kernel.Register(Component.For(typeof(DefaultTemplateEngine)).Named("templateengine"));

            Kernel.AddChildKernel(subkernel);
            subkernel.Register(Component.For(typeof(DefaultTemplateEngine)).Named("templateengine"));

            var templateengine     = Kernel.Resolve <DefaultTemplateEngine>("templateengine");
            var sub_templateengine = subkernel.Resolve <DefaultTemplateEngine>("templateengine");

            var spamservice = subkernel.Resolve <DefaultSpamService>("spamservice");

            Assert.AreNotEqual(spamservice.TemplateEngine, sub_templateengine);
            Assert.AreEqual(spamservice.TemplateEngine, templateengine);

            spamservice = Kernel.Resolve <DefaultSpamService>("spamservice");
            Assert.AreNotEqual(spamservice.TemplateEngine, sub_templateengine);
            Assert.AreEqual(spamservice.TemplateEngine, templateengine);
        }