protected void Application_Start(object sender, EventArgs e) {
     var kernel = new DefaultKernel();
     kernel.Register(Component.For<HttpContextBase>()
                         .LifeStyle.Transient
                         .UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
     kernel.Register(Component.For<SomeService>()
         .LifeStyle.HybridPerWebRequestTransient());
     kernel.Resolve<SomeService>();
 }
Beispiel #2
0
        public void tt()
        {
            var kernel = new DefaultKernel();
            kernel.Register(AllTypes.Pick()
                                   .FromAssembly(Assembly.GetExecutingAssembly())
                                   .WithService.FirstInterface());

            kernel.Resolve<ISomeInterface>();
        }
 public void Kernel_should_select_ctor_with_available_dependencies()
 {
     var kernel = new DefaultKernel();
     kernel.AddComponent<Service>();
     kernel.AddComponent<ComponentX>();
     kernel.AddComponent<ComponentY>();
     var service = kernel.Resolve<Service>();
     Assert.IsNull(service.A);
     Assert.IsNotNull(service.X);
     Assert.IsNotNull(service.Y);
 }
        public void DoesNotResolveArraysByDefault()
        {
            var kernel = new DefaultKernel();

            kernel.Register(
                Component.For<Thing>(),
                Component.For<ISubThing>().ImplementedBy<First>(),
                Component.For<ISubThing>().ImplementedBy<Second>(),
                Component.For<ISubThing>().ImplementedBy<Third>()
                );

            var thing = kernel.Resolve<Thing>();
        }
Beispiel #5
0
		public void SetUp()
		{
			kernel = new DefaultKernel();

			kernel.AddFacility<StartableFacility>();

			kernel.Register(
				Component.For<StartableDisposableAndInitializableComponent>()
					.LifeStyle.Transient
				);

			component = kernel.Resolve<StartableDisposableAndInitializableComponent>();
			component.DoSomething();
			kernel.ReleaseComponent(component);

			calledMethods = component.calledMethods;
		}
        public void ShouldResolveArrayOfDependencies()
        {
            var kernel = new DefaultKernel();
            kernel.Resolver.AddSubResolver(new ArrayResolver(kernel));

            kernel.Register(
                Component.For<Thing>(),
                Component.For<ISubThing>().ImplementedBy<First>(),
                Component.For<ISubThing>().ImplementedBy<Second>(),
                Component.For<ISubThing>().ImplementedBy<Third>()
                );

            var thing = kernel.Resolve<Thing>();

            Assert.That(thing.SubThings.Count, Is.EqualTo(3));
            Assert.That(thing.SubThings[0], Is.InstanceOf(typeof(First)));
            Assert.That(thing.SubThings[1], Is.InstanceOf(typeof(Second)));
            Assert.That(thing.SubThings[2], Is.InstanceOf(typeof(Third)));
        }
		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 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 Singleton_withNonSingletonDependencies_doesNotReResolveDependencies()
		{
			Kernel.Register(Component.For(typeof(DefaultSpamService)).Named("spamservice"));
			Kernel.Register(Component.For(typeof(DefaultMailSenderService)).Named("mailsender"));

			var subkernel1 = new DefaultKernel();
			subkernel1.Register(Component.For(typeof(DefaultTemplateEngine)).Named("templateengine"));
			Kernel.AddChildKernel(subkernel1);

			var subkernel2 = new DefaultKernel();
			subkernel2.Register(Component.For(typeof(DefaultTemplateEngine)).Named("templateengine")
				                    .LifeStyle.Is(LifestyleType.Transient));
			Kernel.AddChildKernel(subkernel2);

			var templateengine1 = subkernel1.Resolve<DefaultTemplateEngine>("templateengine");
			var spamservice1 = subkernel1.Resolve<DefaultSpamService>("spamservice");

			Assert.IsNull(spamservice1.TemplateEngine);

			var templateengine2 = subkernel2.Resolve<DefaultTemplateEngine>("templateengine");
			var spamservice2 = subkernel2.Resolve<DefaultSpamService>("spamservice");

			Assert.AreSame(spamservice1, spamservice2);
		}
		public void DependenciesSatisfiedAmongContainersUsingEvents()
		{
			IKernel subkernel = new DefaultKernel();

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

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

			Kernel.AddChildKernel(subkernel);

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

			Assert.IsNotNull(spamservice);
			Assert.IsNotNull(spamservice.MailSender);
			Assert.IsNotNull(spamservice.TemplateEngine);
		}
		public void Requesting_parent_component_with_child_dependency_from_child_component()
		{
			var subkernel = new DefaultKernel();
			Kernel.AddChildKernel(subkernel);

			Kernel.Register(Component.For<UsesSimpleComponent1>());
			subkernel.Register(Component.For<SimpleComponent1>());

			subkernel.Resolve<UsesSimpleComponent1>();
		}
		public void TestStartableCustomDependencies()
		{
			IKernel kernel = new DefaultKernel();
			kernel.ComponentCreated += OnStartableComponentStarted;

			kernel.AddFacility("startable", new StartableFacility());

			kernel.Register(
				Component.For<StartableComponentCustomDependencies>()
					.Named("a")
					.DependsOn(Property.ForKey("config").Eq(1))
				);
			Assert.IsTrue(startableCreatedBeforeResolved, "Component was not properly started");

			var component = kernel.Resolve<StartableComponentCustomDependencies>("a");

			Assert.IsNotNull(component);
			Assert.IsTrue(component.Started);
			Assert.IsFalse(component.Stopped);

			kernel.ReleaseComponent(component);
			Assert.IsTrue(component.Stopped);
		}
		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 TestStartableWithRegisteredCustomDependencies()
		{
			IKernel kernel = new DefaultKernel();
			kernel.ComponentCreated += OnStartableComponentStarted;

			kernel.AddFacility("startable", new StartableFacility());

			var dependencies = new Dictionary<string, object> { { "config", 1 } };
			kernel.Register(Component.For(typeof(StartableComponentCustomDependencies)).Named("a"));
			kernel.RegisterCustomDependencies(typeof(StartableComponentCustomDependencies), dependencies);

			Assert.IsTrue(startableCreatedBeforeResolved, "Component was not properly started");

			var component = kernel.Resolve<StartableComponentCustomDependencies>("a");

			Assert.IsNotNull(component);
			Assert.IsTrue(component.Started);
			Assert.IsFalse(component.Stopped);

			kernel.ReleaseComponent(component);
			Assert.IsTrue(component.Stopped);
		}
		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);
		}
		public void TestComponentWithNoInterface()
		{
			IKernel kernel = new DefaultKernel();
			kernel.ComponentCreated += OnNoInterfaceStartableComponentStarted;

			var compNode = new MutableConfiguration("component");
			compNode.Attributes["id"] = "b";
			compNode.Attributes["startable"] = "true";
			compNode.Attributes["startMethod"] = "Start";
			compNode.Attributes["stopMethod"] = "Stop";

			kernel.ConfigurationStore.AddComponentConfiguration("b", compNode);

			kernel.AddFacility("startable", new StartableFacility());
			kernel.Register(Component.For(typeof(NoInterfaceStartableComponent)).Named("b"));

			Assert.IsTrue(startableCreatedBeforeResolved, "Component was not properly started");

			var component = kernel.Resolve<NoInterfaceStartableComponent>("b");

			Assert.IsNotNull(component);
			Assert.IsTrue(component.Started);
			Assert.IsFalse(component.Stopped);

			kernel.ReleaseComponent(component);
			Assert.IsTrue(component.Stopped);
		}
		public void TestStartableComponentWithServiceDependency()
		{
			IKernel kernel = new DefaultKernel();
			kernel.ComponentModelBuilder.AddContributor(new ServiceDependencyComponentInspector());
			kernel.ComponentCreated += new ComponentInstanceDelegate(OnStartableComponentWithServiceDependencyStarted);

			kernel.AddFacility("startable", new StartableFacility());

			kernel.Register(Component.For<StartableComponentWithServiceDependency>());

			Assert.IsFalse(startableCreatedBeforeResolved, "Component was started before dependency was registered");

			kernel.Register(Component.For<ServiceDependency>());

			Assert.IsTrue(startableCreatedBeforeResolved, "Component was not properly started");

			StartableComponentWithServiceDependency component = kernel.Resolve<StartableComponentWithServiceDependency>();

			Assert.IsNotNull(component);
			Assert.IsTrue(component.Started);
			Assert.IsFalse(component.Stopped);

			kernel.ReleaseComponent(component);
			Assert.IsTrue(component.Stopped);
		}
		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);
		}
		public void ChildDependenciesIsSatisfiedEvenWhenComponentTakesLongToBeAddedToParentContainer()
		{
			var container = new DefaultKernel();
			var childContainer = new DefaultKernel();

			container.AddChildKernel(childContainer);
			childContainer.Register(Component.For(typeof(UsesIEmptyService)).Named("component"));

			container.Register(
				Component.For(typeof(IEmptyService)).ImplementedBy(typeof(EmptyServiceA)).Named("service1"));

			childContainer.Resolve<UsesIEmptyService>();
		}
		public void ChildKernelFindsAndCreateParentComponent()
		{
			IKernel subkernel = new DefaultKernel();

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

			Kernel.AddChildKernel(subkernel);

			Assert.IsTrue(subkernel.HasComponent(typeof(DefaultTemplateEngine)));
			Assert.IsNotNull(subkernel.Resolve<DefaultTemplateEngine>());
		}
		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 TestInterfaceBasedStartable()
		{
			IKernel kernel = new DefaultKernel();
			kernel.ComponentCreated += OnStartableComponentStarted;

			kernel.AddFacility("startable", new StartableFacility());

			kernel.Register(Component.For(typeof(StartableComponent)).Named("a"));

			Assert.IsTrue(startableCreatedBeforeResolved, "Component was not properly started");

			var component = kernel.Resolve<StartableComponent>("a");

			Assert.IsNotNull(component);
			Assert.IsTrue(component.Started);
			Assert.IsFalse(component.Stopped);

			kernel.ReleaseComponent(component);
			Assert.IsTrue(component.Stopped);
		}