Default implementation of IKernel. This implementation is complete and also support a kernel hierarchy (sub containers).
		public void TestComponentWithNoInterface()
		{
			IKernel kernel = new DefaultKernel();
			kernel.ComponentCreated += new ComponentInstanceDelegate(OnNoInterfaceStartableComponentStarted);

			MutableConfiguration 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");

			NoInterfaceStartableComponent component = kernel["b"] as NoInterfaceStartableComponent;

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

			kernel.ReleaseComponent(component);
			Assert.IsTrue(component.Stopped);
		}
Ejemplo n.º 2
0
 public static ICollection<IHandler> SimulateRegistration(this IRegistration reg)
 {
     using (var kernel = new DefaultKernel()) {
         kernel.Register(reg);
         return kernel.GetAssignableHandlers(typeof (object));
     }
 }
		public void MissingManifestResourceConfiguration()
		{
			var store = new DefaultConfigurationStore();
			var source = new AssemblyResource("assembly://Castle.Windsor.Tests/missing_config.xml");
			IKernel kernel = new DefaultKernel();
			new XmlInterpreter(source).ProcessResource(source, store, kernel);
		}
		public void ProperDeserialization()
		{
			var store = new DefaultConfigurationStore();
			var interpreter = new XmlInterpreter(new ConfigResource());
			IKernel kernel = new DefaultKernel();
			interpreter.ProcessResource(interpreter.Source, store, kernel);

			Assert.AreEqual(2, store.GetFacilities().Length);
			Assert.AreEqual(4, store.GetComponents().Length);

			var config = store.GetFacilityConfiguration(typeof(NoopFacility).FullName);
			var childItem = config.Children["item"];
			Assert.IsNotNull(childItem);
			Assert.AreEqual("value", childItem.Value);

			config = store.GetFacilityConfiguration(typeof(Noop2Facility).FullName);
			Assert.IsNotNull(config);
			Assert.AreEqual("value within CDATA section", config.Value);

			config = store.GetComponentConfiguration("testidcomponent1");
			childItem = config.Children["item"];
			Assert.IsNotNull(childItem);
			Assert.AreEqual("value1", childItem.Value);

			config = store.GetComponentConfiguration("testidcomponent2");
			childItem = config.Children["item"];
			Assert.IsNotNull(childItem);
			Assert.AreEqual("value2", childItem.Value);
		}
		public void Starts_component_without_start_method()
		{
			ClassWithInstanceCount.InstancesCount = 0;
			IKernel kernel = new DefaultKernel();
			kernel.AddFacility<StartableFacility>(f => f.DeferredTryStart());
			kernel.Register(Component.For<ClassWithInstanceCount>().Start());
			Assert.AreEqual(1, ClassWithInstanceCount.InstancesCount);
		}
Ejemplo n.º 6
0
        public void tt()
        {
            var kernel = new DefaultKernel();
            kernel.Register(AllTypes.Pick()
                                   .FromAssembly(Assembly.GetExecutingAssembly())
                                   .WithService.FirstInterface());

            kernel.Resolve<ISomeInterface>();
        }
        public void current_property_exposes_the_correct_resolver()
        {
            var kernel = new DefaultKernel();
            var resolver = new CastleDependencyResolver(kernel);

            GlobalHost.DependencyResolver = resolver;

            Assert.That(CastleDependencyResolver.Current, Is.EqualTo(GlobalHost.DependencyResolver));
        }
        public void get_service_returns_null_for_unregistered_service()
        {
            var kernel = new DefaultKernel();
            var resolver = new CastleDependencyResolver(kernel);

            var service = resolver.GetService(typeof(object));

            Assert.That(service, Is.Null);
        }
 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>();
 }
        public void get_services_returns_empty_enumerable_for_unregistered_service()
        {
            var kernel = new DefaultKernel();
            var resolver = new CastleDependencyResolver(kernel);

            var services = resolver.GetServices(typeof(object));

            Assert.That(services.Count(), Is.EqualTo(0));
        }
Ejemplo n.º 11
0
		public static void Main(string[] args)
		{
			IKernel kernel = new DefaultKernel();
			
			kernel.AddFacility("non.opt.props", new NonOptionalPropertiesFacility());
			
			kernel.AddComponent("sql.connmng", typeof(SqlConnectionManager));
			
			SqlConnectionManager connManager = (SqlConnectionManager) kernel["sql.connmng"];
		}
        public void get_services_returns_registered_service()
        {
            var kernel = new DefaultKernel();
            kernel.Register(Component.For<object>().Instance(new object()));

            var resolver = new CastleDependencyResolver(kernel);

            var services = resolver.GetServices(typeof(object));

            Assert.That(services.Count(), Is.EqualTo(1));
        }
Ejemplo n.º 13
0
		public void AddChildKernelToTwoParentsThrowsException()
		{
			IKernel kernel2 = new DefaultKernel();

			IKernel subkernel = new DefaultKernel();

			Kernel.AddChildKernel(subkernel);
			Assert.AreEqual(Kernel, subkernel.Parent);

			kernel2.AddChildKernel(subkernel);
		}
Ejemplo n.º 14
0
 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);
 }
Ejemplo n.º 15
0
        public virtual void Should_Dispose_From_Containers()
        {
            DefaultKernel disposableKernel = new DefaultKernel();
            using (var disposableLocater = new SiegeContainer(new WindsorAdapter.WindsorAdapter(disposableKernel)))
            {
                disposableLocater.Register(Given<ITestInterface>.Then<TestCase1>());
                Assert.IsTrue(disposableLocater.GetInstance<ITestInterface>() is TestCase1);
            }

            Assert.IsFalse(disposableKernel.HasComponent(typeof(ITestInterface)));
        }
		public void CorrectConfigurationMapping()
		{
			var store = new DefaultConfigurationStore();
			var interpreter = new XmlInterpreter(Xml.Embedded("sample_config.xml"));
			IKernel kernel = new DefaultKernel();
			interpreter.ProcessResource(interpreter.Source, store, kernel);

			var container = new WindsorContainer(store);
			var facility = container.Kernel.GetFacilities().OfType<HiperFacility>().Single();
			Assert.IsTrue(facility.Initialized);
		}
		public void ComponentIdGetsLoadedFromTheParsedConfiguration()
		{
			var store = new DefaultConfigurationStore();
			var interpreter = new XmlInterpreter(Xml.Embedded("sample_config_with_spaces.xml"));
			IKernel kernel = new DefaultKernel();
			interpreter.ProcessResource(interpreter.Source, store, kernel);

			var container = new WindsorContainer(store);

			var handler = container.Kernel.GetHandler(typeof(ICalcService));
			Assert.AreEqual(Core.LifestyleType.Transient, handler.ComponentModel.LifestyleType);
		}
Ejemplo n.º 18
0
		public void FacilityConfig_is_not_null()
		{
			using (var c = new DefaultKernel())
			{
				const string facilityKey = "hiper";
				var config = new MutableConfiguration("facility");
				c.ConfigurationStore.AddFacilityConfiguration(facilityKey, config);
				var facility = new HiperFacility();
				c.AddFacility(facilityKey, facility);
				Assert.IsTrue(facility.Initialized);
			}
		}
Ejemplo n.º 19
0
        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>();
        }
		public void LoadWindsorAssembly()
		{
			IKernel kernel = new DefaultKernel();
			kernel.AddComponent( "A", typeof(AssemblyResolverComponent) );
			
			AssemblyResolverComponent comp = (AssemblyResolverComponent) kernel["A"];
			comp.Start();

			Type windsor = Type.GetType( 
				"Castle.Windsor.WindsorContainer, Castle.Windsor", false, false );
			
			Assert.IsNotNull(windsor);
		}
Ejemplo n.º 21
0
        public void DoesNotDiscoverCircularDependencies()
        {
            var kernel = new DefaultKernel();
            kernel.Resolver.AddSubResolver(new ArrayResolver(kernel));

            // a circular reference exception should be thrown here
            kernel.Register(
                Component.For<Thing>(),
                Component.For<ISubThing>().ImplementedBy<Circular>()
                );

            // this crashes the test framework!
            // var thing = kernel.Resolve<Thing>();
        }
        public void LeaseIsCorrectWhenAccessedAfterInitializationButNoLeaseIsAvailableInKernel()
        {
            // Fixture setup
            var kernel = new DefaultKernel();
            var model = new ComponentModel("foo", typeof(ICloneable), typeof(Version));
            var activator = kernel.CreateComponentActivator(model);

            var sut = new CacheLifestyleManager();
            sut.Init(activator, kernel, model);
            // Exercise system
            var result = sut.Lease;
            // Verify outcome
            var actual = Assert.IsAssignableFrom<SlidingLease>(result);
            Assert.Equal(TimeSpan.FromMinutes(1), actual.Timeout);
            // Teardown
        }
        public static IKernel Build()
        {
            var kernel = new DefaultKernel();
            kernel.Resolver.AddSubResolver(new CollectionResolver(kernel, true));

            RegisterBackupSources(kernel);
            RegisterCore(kernel);
            RegisterDatabaseProviders(kernel);
            RegisterEmail(kernel);
            RegisterLoggers(kernel);
            RegisterSevenZip(kernel);
            RegisterStorageSources(kernel);
            RegisterUserDataProviders(kernel);

            return kernel;
        }
Ejemplo n.º 24
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 LeaseIsCorrectWhenAccessedAfterInitializationWhenLeaseIsAvailabeInKernel()
        {
            // Fixture setup
            var kernel = new DefaultKernel();
            var model = new ComponentModel("foo", typeof(ICloneable), typeof(Version));
            var activator = kernel.CreateComponentActivator(model);

            var expectedLease = new Mock<ILease> { DefaultValue = DefaultValue.Mock }.Object;
            kernel.Register(Component.For<ILease>().Instance(expectedLease));

            var sut = new CacheLifestyleManager();
            sut.Init(activator, kernel, model);
            // Exercise system
            var result = sut.Lease;
            // Verify outcome
            Assert.Equal(expectedLease, result);
            // Teardown
        }
Ejemplo n.º 26
0
        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 TestInterfaceBasedStartable()
		{
			IKernel kernel = new DefaultKernel();
			kernel.ComponentCreated += new ComponentInstanceDelegate(OnStartableComponentStarted);

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

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

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

			StartableComponent component = kernel["a"] as StartableComponent;

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

			kernel.ReleaseComponent(component);
			Assert.IsTrue(component.Stopped);
		}
		public void ProperDeserialization()
		{
			var store = new DefaultConfigurationStore();

			var interpreter = new XmlInterpreter(Xml.Embedded("sample_config_complex.xml"));
			IKernel kernel = new DefaultKernel();
			interpreter.ProcessResource(interpreter.Source, store, kernel);

			Assert.AreEqual(2, store.GetFacilities().Length);
			Assert.AreEqual(2, store.GetComponents().Length);
			Assert.AreEqual(2, store.GetConfigurationForChildContainers().Length);

			var config = store.GetFacilityConfiguration(typeof(DummyFacility).FullName);
			var childItem = config.Children["item"];
			Assert.IsNotNull(childItem);
			Assert.AreEqual("value", childItem.Value);

			config = store.GetFacilityConfiguration(typeof(HiperFacility).FullName);
			Assert.IsNotNull(config);
			Assert.AreEqual("value within CDATA section", config.Value);

			config = store.GetComponentConfiguration("testidcomponent1");
			childItem = config.Children["item"];
			Assert.IsNotNull(childItem);
			Assert.AreEqual("value1", childItem.Value);

			config = store.GetComponentConfiguration("testidcomponent2");
			childItem = config.Children["item"];
			Assert.IsNotNull(childItem);
			Assert.AreEqual("value2", childItem.Value);

			config = store.GetChildContainerConfiguration("child1");
			Assert.IsNotNull(config);
			Assert.AreEqual(config.Attributes["name"], "child1");
			Assert.AreEqual("<configuration />", config.Value);

			config = store.GetChildContainerConfiguration("child2");
			Assert.IsNotNull(config);
			Assert.AreEqual(config.Attributes["name"], "child2");
			Assert.AreEqual("<configuration />", config.Value);
		}
Ejemplo n.º 29
0
		private IKernel CreateConfiguredKernel()
		{
			IKernel kernel = new DefaultKernel();

			MutableConfiguration confignode = new MutableConfiguration("facility");
	
			IConfiguration engines = 
				confignode.Children.Add( new MutableConfiguration("engines") );
	
			IConfiguration engine = 
				engines.Children.Add( new MutableConfiguration("engine") );
	
			engine.Attributes["id"] = "engineid";
			engine.Attributes["systemId"] = "systemid";
			engine.Attributes["systemType"] = typeof(UserDatabase).AssemblyQualifiedName;
			engine.Attributes["storageDir"] = _storageDir;

			kernel.ConfigurationStore.AddFacilityConfiguration( "prevalence", confignode );
			
			return kernel;
		}
Ejemplo n.º 30
0
        private static void SimpleRegistration()
        {
            IKernel kernel = new DefaultKernel();

            //kernel.Resolver.AddSubResolver(new ArrayResolver(kernel));
            //kernel.Resolver.AddSubResolver(new ListResolver(kernel));

            kernel.AddComponent("foo", typeof(IFoo), typeof(Foo));

            IFoo foo = kernel[typeof(IFoo)] as IFoo;

            if (foo != null)
            {
                foo.SayHello();
            }

            IFoo foo2 = kernel["foo"] as IFoo;
            if (foo2 != null)
            {
                foo2.SayHello();
            }
        }
Ejemplo n.º 31
0
 public OptimizeDependencyResolutionDisposable(DefaultKernel kernel)
 {
     this.kernel = kernel;
 }