public void AddingToTwoParentContainsThrowsKernelException() { IWindsorContainer container3 = new WindsorContainer(); IWindsorContainer childcontainer = new WindsorContainer(); Container.AddChildContainer(childcontainer); container3.AddChildContainer(childcontainer); }
private static void RegisterContainers() { ICalculatorContainer calculatorContainer = new Container(); var calculatorChildContainer = calculatorContainer.RegisterComponents(); Container.AddChildContainer(calculatorChildContainer); }
public void AddingToTwoParentContainsThrowsKernelException() { IWindsorContainer container3 = new WindsorContainer(); IWindsorContainer childcontainer = new WindsorContainer(); Container.AddChildContainer(childcontainer); Assert.Throws <KernelException>(() => container3.AddChildContainer(childcontainer)); }
public void ShouldResolveComponentFromParent() { WindsorContainer parent = new WindsorContainer(); WindsorContainer child = new WindsorContainer(); parent.AddChildContainer(child); parent.AddComponent("DoNothingService", typeof(IDoNothingService), typeof(DoNothingService)); child.AddComponent("DoSomethingService", typeof(IDoSomethingService), typeof(DoSomethingService)); Assert.IsNotNull(child.Resolve<IDoNothingService>()); Assert.IsNotNull(child.Resolve<IDoSomethingService>()); }
public void ShouldResolveComponentFromParent() { WindsorContainer parent = new WindsorContainer(); WindsorContainer child = new WindsorContainer(); parent.AddChildContainer(child); ((IWindsorContainer)parent).Register(Component.For(typeof(IDoNothingService)).ImplementedBy(typeof(DoNothingService)).Named("DoNothingService")); ((IWindsorContainer)child).Register(Component.For(typeof(IDoSomethingService)).ImplementedBy(typeof(DoSomethingService)).Named("DoSomethingService")); Assert.IsNotNull(child.Resolve<IDoNothingService>()); Assert.IsNotNull(child.Resolve<IDoSomethingService>()); }
public void ShouldResolveDecoratedComponentFromParent() { WindsorContainer parent = new WindsorContainer(); WindsorContainer child = new WindsorContainer(); parent.AddChildContainer(child); parent.AddComponent("DoNothingServiceDecorator", typeof(IDoNothingService), typeof(DoNothingServiceDecorator)); parent.AddComponent("DoNothingService", typeof(IDoNothingService), typeof(DoNothingService)); child.AddComponent("DoSometingService", typeof(IDoSomethingService), typeof(DoSomethingService)); IDoNothingService service = child.Resolve<IDoNothingService>(); Assert.IsNotNull(service); Assert.IsInstanceOf(typeof(DoNothingServiceDecorator), service); }
public void ShouldResolveComponentFromParent() { var parent = new WindsorContainer(); var child = new WindsorContainer(); parent.AddChildContainer(child); parent.Register( Component.For<IDoNothingService>().ImplementedBy<DoNothingService>().Named("DoNothingService"), Component.For<IDoSomethingService>().ImplementedBy<DoSomethingService>().Named("DoSomethingService")); Assert.IsNotNull(child.Resolve<IDoNothingService>()); Assert.IsNotNull(child.Resolve<IDoSomethingService>()); }
public void Cannot_resolve_a_dependency_from_a_parent_container_under_certain_circumstances() { var parent = new WindsorContainer(); var child = new WindsorContainer(); parent.AddChildContainer(child); ((IWindsorContainer)parent).Register(Component.For(typeof(IParentService)).ImplementedBy(typeof(ParentService)).Named("service")); ((IWindsorContainer)child).Register(Component.For(typeof(IChildService1)).ImplementedBy(typeof(ChildService1)).Named("service1")); ((IWindsorContainer)child).Register(Component.For(typeof(IChildService2)).ImplementedBy(typeof(ChildService2)).Named("service2")); child.Resolve <IChildService1>(); }
public void Cannot_resolve_a_dependency_from_a_parent_container_under_certain_circumstances() { var parent = new WindsorContainer(); var child = new WindsorContainer(); parent.AddChildContainer(child); ((IWindsorContainer)parent).Register(Component.For(typeof(IParentService)).ImplementedBy(typeof(ParentService)).Named("service")); ((IWindsorContainer)child).Register(Component.For(typeof(IChildService1)).ImplementedBy(typeof(ChildService1)).Named("service1")); ((IWindsorContainer)child).Register(Component.For(typeof(IChildService2)).ImplementedBy(typeof(ChildService2)).Named("service2")); child.Resolve<IChildService1>(); }
public void ShouldResolveComponentFromParent() { var parent = new WindsorContainer(); var child = new WindsorContainer(); parent.AddChildContainer(child); parent.Register( Component.For <IDoNothingService>().ImplementedBy <DoNothingService>().Named("DoNothingService"), Component.For <IDoSomethingService>().ImplementedBy <DoSomethingService>().Named("DoSomethingService")); Assert.IsNotNull(child.Resolve <IDoNothingService>()); Assert.IsNotNull(child.Resolve <IDoSomethingService>()); }
public void ShouldResolveDecoratedComponentFromGrandParent() { WindsorContainer grandParent = new WindsorContainer(); WindsorContainer parent = new WindsorContainer(); WindsorContainer child = new WindsorContainer(); grandParent.AddChildContainer(parent); parent.AddChildContainer(child); ((IWindsorContainer)grandParent).Register(Component.For(typeof(IDoNothingService)).ImplementedBy(typeof(DoNothingServiceDecorator)).Named("DoNothingServiceDecorator")); ((IWindsorContainer)grandParent).Register(Component.For(typeof(IDoNothingService)).ImplementedBy(typeof(DoNothingService)).Named("DoNothingService")); IDoNothingService service = child.Resolve<IDoNothingService>(); Assert.IsNotNull(service); Assert.IsInstanceOf(typeof(DoNothingServiceDecorator), service); }
public void Should_resolve_child_from_childs_container() { WindsorContainer parent = new WindsorContainer(); WindsorContainer child = new WindsorContainer(); parent.AddChildContainer(child); ((IWindsorContainer)parent).Register(Component.For(typeof(IParentService)).ImplementedBy(typeof(ParentService)).Named("service1")); ((IWindsorContainer)parent).Register(Component.For(typeof(IChildService2)).ImplementedBy(typeof(ChildService2)).Named("service3")); ((IWindsorContainer)child).Register(Component.For(typeof(IParentService)).ImplementedBy(typeof(AnotherParentService)).Named("service2")); IChildService2 resolve = child.Resolve<IChildService2>(); Assert.IsInstanceOf(typeof(AnotherParentService),resolve.Parent); }
public void Shows_also_components_from_parent_container() { var parent = new WindsorContainer(); parent.Register(Component.For<A>(), Component.For<B>()); Container.Register(Component.For(typeof(IGeneric<>)).ImplementedBy(typeof(GenericImpl1<>)), Component.For(typeof(IGeneric<>)).ImplementedBy(typeof(GenericImpl2<>))); parent.AddChildContainer(Container); var handlers = diagnostic.Inspect(); Assert.AreEqual(4, handlers.Length); }
public void Should_resolve_child_from_childs_container() { WindsorContainer parent = new WindsorContainer(); WindsorContainer child = new WindsorContainer(); parent.AddChildContainer(child); parent.AddComponent("service1", typeof(IParentService), typeof(ParentService)); parent.AddComponent("service3", typeof(IChildService2), typeof(ChildService2)); child.AddComponent("service2", typeof(IParentService), typeof(AnotherParentService)); IChildService2 resolve = child.Resolve<IChildService2>(); Assert.IsInstanceOf(typeof(AnotherParentService),resolve.Parent); }
public void Cannot_resolve_a_dependency_from_a_parent_container_under_certain_circumstances() { WindsorContainer parent = new WindsorContainer(); WindsorContainer child = new WindsorContainer(); parent.AddChildContainer(child); parent.AddComponent("service", typeof(IParentService), typeof(ParentService)); child.AddComponent("service1", typeof(IChildService1), typeof(ChildService1)); child.AddComponent("service2", typeof(IChildService2), typeof(ChildService2)); child.Resolve<IChildService1>(); }
public void Shows_also_components_from_parent_container() { var parent = new WindsorContainer(); parent.Register(Component.For <A>(), Component.For <B>()); Container.Register(Component.For(typeof(IGeneric <>)).ImplementedBy(typeof(GenericImpl1 <>)), Component.For(typeof(IGeneric <>)).ImplementedBy(typeof(GenericImpl2 <>))); parent.AddChildContainer(Container); var handlers = diagnostic.Inspect(); Assert.AreEqual(4, handlers.Length); }
public void AddComponentInstanceAndChildContainers() { IWindsorContainer parent = new WindsorContainer(); IWindsorContainer child = new WindsorContainer(); parent.AddChildContainer(child); IEmptyService clock1 = new EmptyServiceA(); IEmptyService clock2 = new EmptyServiceB(); parent.Kernel.Register(Component.For(typeof(IEmptyService)).Instance(clock2)); child.Kernel.Register(Component.For(typeof(IEmptyService)).Instance(clock1)); Assert.AreSame(clock2, parent.Resolve<IEmptyService>()); Assert.AreSame(clock1, child.Resolve<IEmptyService>()); }
public void Should_resolve_child_from_childs_container() { var parent = new WindsorContainer(); var child = new WindsorContainer(); parent.AddChildContainer(child); ((IWindsorContainer)parent).Register(Component.For(typeof(IParentService)).ImplementedBy(typeof(ParentService)).Named("service1")); ((IWindsorContainer)parent).Register(Component.For(typeof(IChildService2)).ImplementedBy(typeof(ChildService2)).Named("service3")); ((IWindsorContainer)child).Register(Component.For(typeof(IParentService)).ImplementedBy(typeof(AnotherParentService)).Named("service2")); var resolve = child.Resolve <IChildService2>(); Assert.IsInstanceOf(typeof(AnotherParentService), resolve.Parent); }
public void AddComponentInstanceAndChildContainers() { IWindsorContainer parent = new WindsorContainer(); IWindsorContainer child = new WindsorContainer(); parent.AddChildContainer(child); IClock clock1 = new IsraelClock(); IClock clock2 = new WorldClock(); parent.Kernel.AddComponentInstance<IClock>(clock2); child.Kernel.AddComponentInstance<IClock>(clock1); Assert.AreSame(clock2,parent.Resolve<IClock>()); Assert.AreSame(clock1, child.Resolve<IClock>()); }
public void TestForSerivces() { using (var container = new WindsorContainer()) { container.Register(Component.For <IInterface>().ImplementedBy <InterfaceImpl>()); IInterface childInterface; using (var childContainer = new WindsorContainer()) { container.AddChildContainer(childContainer); childInterface = container.Resolve <IInterface>(); } // childIhterface is NOT disposing here var @interface = container.Resolve <IInterface>(); Assert.AreSame(@interface, childInterface); @interface.Do(); } // but is disposing here and this is right behavior }
public void TestForSerivces() { using (var container = new WindsorContainer()) { container.Register(Component.For<IInterface>().ImplementedBy<InterfaceImpl>()); IInterface childInterface; using (var childContainer = new WindsorContainer()) { container.AddChildContainer(childContainer); childInterface = container.Resolve<IInterface>(); } // childIhterface is NOT disposing here var @interface = container.Resolve<IInterface>(); Assert.AreSame(@interface, childInterface); @interface.Do(); } // but is disposing here and this is right behavior }
public void AddComponentInstanceAndChildContainers() { IWindsorContainer parent = new WindsorContainer(); IWindsorContainer child = new WindsorContainer(); parent.AddChildContainer(child); IEmptyService clock1 = new EmptyServiceA(); IEmptyService clock2 = new EmptyServiceB(); parent.Kernel.Register(Component.For(typeof(IEmptyService)).Instance(clock2)); child.Kernel.Register(Component.For(typeof(IEmptyService)).Instance(clock1)); Assert.AreSame(clock2, parent.Resolve <IEmptyService>()); Assert.AreSame(clock1, child.Resolve <IEmptyService>()); }
public void ShouldResolveDecoratedComponentFromParent() { var parent = new WindsorContainer(); var child = new WindsorContainer(); parent.AddChildContainer(child); parent.Register( Component.For(typeof(IDoNothingService)).ImplementedBy(typeof(DoNothingServiceDecorator)).Named( "DoNothingServiceDecorator")); parent.Register( Component.For(typeof(IDoNothingService)).ImplementedBy(typeof(DoNothingService)).Named("DoNothingService")); child.Register( Component.For(typeof(IDoSomethingService)).ImplementedBy(typeof(DoSomethingService)).Named("DoSometingService")); var service = child.Resolve <IDoNothingService>(); Assert.IsNotNull(service); Assert.IsInstanceOf(typeof(DoNothingServiceDecorator), service); }
public void TestForTypedFactories() { using (var container = new WindsorContainer()) { container.AddFacility <TypedFactoryFacility>(); container.Register(Component.For <IFactory>().AsFactory(), Component.For(typeof(IInterface)).ImplementedBy(typeof(InterfaceImpl)).LifeStyle.Transient); IFactory childFactory; using (var childContainer = new WindsorContainer()) { container.AddChildContainer(childContainer); childFactory = childContainer.Resolve <IFactory>(); } // childFactory is disposing here var factory = container.Resolve <IFactory>(); Assert.AreSame(factory, childFactory); Assert.DoesNotThrow(() => factory.Create()); // throws an ObjectDisposedException exception } // but should be disposed here }
public void TestForTypedFactories() { using (var container = new WindsorContainer()) { container.AddFacility<TypedFactoryFacility>(); container.Register(Component.For<IFactory>().AsFactory(), Component.For(typeof(IInterface)).ImplementedBy(typeof(InterfaceImpl)).LifeStyle.Transient); IFactory childFactory; using (var childContainer = new WindsorContainer()) { container.AddChildContainer(childContainer); childFactory = childContainer.Resolve<IFactory>(); } // childFactory is disposing here var factory = container.Resolve<IFactory>(); Assert.AreSame(factory, childFactory); Assert.DoesNotThrow(() => factory.Create()); // throws an ObjectDisposedException exception } // but should be disposed here }
public void test() { using (var parentContainer = new WindsorContainer()) { parentContainer.Register(Component.For<Service1>().LifeStyle.Transient); for (var i = 0; i < 100000; i++) { using (IWindsorContainer childContainer = new WindsorContainer()) { //childContainer.Kernel.ReleasePolicy = new LifecycledComponentsReleasePolicy(); parentContainer.AddChildContainer(childContainer); var service1 = childContainer.Resolve<Service1>(); childContainer.Release(service1); parentContainer.Release(service1); if (i%100 == 0) { GC.Collect(2); Console.WriteLine("Iteration: {0}, Memory {1}", i, GC.GetTotalMemory(false)); } parentContainer.RemoveChildContainer(childContainer); } } } }
static void Main() { var container = new WindsorContainer(); container.Register( Component.For <ISender>() .ImplementedBy <EmailSender>(), Component.For <IUsersService>() .ImplementedBy <DefaultUsersService>() ); var childContainer = new WindsorContainer(); childContainer.Register( Component.For <ISender>() .ImplementedBy <SmsSender>() ); container.AddChildContainer(childContainer); Console.WriteLine("Getting sender component"); var sms1 = container.Resolve <ISender>(); Console.WriteLine(sms1); var sms2 = childContainer.Resolve <ISender>(); Console.WriteLine(sms2); Console.WriteLine(); Console.WriteLine("Getting user service"); // trying to get a component defined in the parent container // var usersService1 = container.Resolve<IUsersService>(); // Console.WriteLine(usersService1.Sender); var usersService2 = childContainer.Resolve <IUsersService>(); Console.WriteLine(usersService2.Sender); }
public void DisposeChildContainerComponentsResolvedAgainstParentContainer() { IWindsorContainer parentContainer = new WindsorContainer(); //parentContainer.Kernel.ReleasePolicy = new MyCustomReleasePolicy(); IWindsorContainer childContainer = new WindsorContainer(); parentContainer.AddChildContainer(childContainer); parentContainer.Register(Component.For <DisposableFoo>().ImplementedBy <DisposableFoo>()); var first = childContainer.Resolve <DisposableFoo>(); Assert.IsNotNull(first); childContainer.Release(first); Assert.Equals(1, DisposableFoo.DisposedCount); var second = childContainer.Resolve <DisposableFoo>(); Assert.IsNotNull(second); childContainer.Dispose(); Assert.Equals(2, DisposableFoo.DisposedCount); }
public void Facility_When_added_to_a_child_container_wher_parent_has_facility_pulls_from_child() { var mainContainer = new WindsorContainer(); var childContainer = new WindsorContainer(); // NOTE: this has to happen in this order mainContainer.AddChildContainer(childContainer); mainContainer.AddFacility <TypedFactoryFacility>(); childContainer.AddFacility <TypedFactoryFacility>(); mainContainer.Register(Component.For <IDummyComponent>().ImplementedBy <Component1>()); childContainer.Register(Component.For <DummyComponentFactory>().AsFactory(), Component.For <IDummyComponent>().ImplementedBy <Component2>()); var fromParent = mainContainer.Resolve <IDummyComponent>(); var fromFactory = childContainer.Resolve <DummyComponentFactory>().CreateDummyComponent(); var fromChild = childContainer.Resolve <IDummyComponent>(); Assert.AreSame(fromFactory, fromChild); Assert.AreNotSame(fromChild, fromParent); Assert.AreNotSame(fromFactory, fromParent); }
public void Facility_When_added_to_a_child_container_wher_parent_has_facility_pulls_from_child() { var mainContainer = new WindsorContainer(); var childContainer = new WindsorContainer(); // NOTE: this has to happen in this order mainContainer.AddChildContainer(childContainer); mainContainer.AddFacility<TypedFactoryFacility>(); childContainer.AddFacility<TypedFactoryFacility>(); mainContainer.Register(Component.For<IDummyComponent>().ImplementedBy<Component1>()); childContainer.Register(Component.For<DummyComponentFactory>().AsFactory(), Component.For<IDummyComponent>().ImplementedBy<Component2>()); var fromParent = mainContainer.Resolve<IDummyComponent>(); var fromFactory = childContainer.Resolve<DummyComponentFactory>().CreateDummyComponent(); var fromChild = childContainer.Resolve<IDummyComponent>(); Assert.AreSame(fromFactory, fromChild); Assert.AreNotSame(fromChild, fromParent); Assert.AreNotSame(fromFactory, fromParent); }
static void Main(string[] args) { string pluginsFolder = Path.GetFullPath(@"..\..\..\Plugins"); AssemblyFilter assemblyFilter = new AssemblyFilter(pluginsFolder); var rootContainer = new WindsorContainer(); rootContainer.Register(Component.For <MyRootService>()); rootContainer.Register(Classes.FromAssemblyInDirectory(assemblyFilter).BasedOn <IDemoPlugin>().WithService.FromInterface()); IDemoPlugin[] plugins = rootContainer.ResolveAll <IDemoPlugin>(); Console.WriteLine($"In folder {pluginsFolder} found {plugins.Length} plugins."); foreach (IDemoPlugin plugin in plugins) { plugin.Run(null); } rootContainer.Resolve <MyRootService>().Run("root"); WindsorContainer childContainer = new WindsorContainer(); childContainer.Register(Component.For <MyChildService>()); rootContainer.AddChildContainer(childContainer); childContainer.Resolve <MyRootService>().Run("child"); childContainer.Resolve <MyChildService>().Run("child"); try { Console.WriteLine("trying to resolve MyChildContainer in root container..."); rootContainer.Resolve <MyChildService>().Run("root"); } catch (Exception e) { Console.WriteLine($"Cannot resolve in root: {e.Message}"); } Console.WriteLine("Press Enter to exit."); Console.ReadLine(); }
static void Main() { var container = new WindsorContainer(); container.Register( Component.For<ISender>() .ImplementedBy<EmailSender>(), Component.For<IUsersService>() .ImplementedBy<DefaultUsersService>() ); var childContainer = new WindsorContainer(); childContainer.Register( Component.For<ISender>() .ImplementedBy<SmsSender>() ); container.AddChildContainer(childContainer); Console.WriteLine("Getting sender component"); var sms1 = container.Resolve<ISender>(); Console.WriteLine(sms1); var sms2 = childContainer.Resolve<ISender>(); Console.WriteLine(sms2); Console.WriteLine(); Console.WriteLine("Getting user service"); // trying to get a component defined in the parent container // var usersService1 = container.Resolve<IUsersService>(); // Console.WriteLine(usersService1.Sender); var usersService2 = childContainer.Resolve<IUsersService>(); Console.WriteLine(usersService2.Sender); }
private void README() { //By default WC create singleton var container = new WindsorContainer(); container.Register(Component.For <ShopperCtrInject>()); //use forward chaining when you card same IMPL for diff interface in case you want a signleton container.Register(Component.For <ICreditCard>().ImplementedBy <MasterCard>() .Forward <ICreditCard>().ImplementedBy <Visa>()); //Not good practice, if you already created an instance you attached to interface var visa = new Visa(); container.Register(Component.For <ICreditCard>().Instance(visa)); //Unity allows specificily how to override the contructor injection in the resolve method VS Windsor Castle (WC)in the Register Methods //WC automatically going to inject on properties, i.e setter injection if you expose a property with an interface and the container //knows about that interface container.Register(Component.For <ICreditCard>().ImplementedBy <MasterCard>().Named("defaultCard").LifeStyle.Transient); container.Register(Component.For <ICreditCard>().ImplementedBy <Visa>().Named("backupCard").LifestyleScoped()); container.Register(Component.For <ICreditCard>().ImplementedBy <Visa>().Named("backupCard2").LifestylePerWebRequest()); container.Register(Component.For <IShopper>().ImplementedBy <ShopperPropInject>()); container.Register(Component.For <IShopper>().ImplementedBy <ShopperCtrInject>().LifestyleTransient()); //this will resolve all credit card, by we get the first one, i.e default mastercard unless we specified by name var creditCard = container.ResolveAll <ICreditCard>(); //e.g logging: we could log whenever a component is created or whenever something is used you could just add it, //facility will allow us to insert certain hooks into the lifecycle of the creation of objects and the usage of objects and the container. //container.Register(Component.For<IShopper>().ImplementedBy<ShopperCtrInject>()).AddFacility(YourLogic()); //Install provides a way to put all registration into one place. //Install modulizes application so we could register multiple registration in each installer and control what gets installed. //now components registration is delegated to the installer. installer accept multiple registration installer... container.Install(new ShoppingInstaller(), new ShoppingInstaller2(), new ShoppingInstaller3()); container.Install(new ShoppingInstaller()); //child containers (not a very widely used feature - NOT recommended) : concept where you could have nested //containers and you could use a child container to control scope or just logical separation in the app, //and child container will try first to resolve its own registered dependencies and if it can't then it'll check the parent. var childContainer = new WindsorContainer(); container.AddChildContainer(childContainer); var shopper = container.Resolve <IShopper>(); shopper.Charge(); Console.WriteLine(shopper.ChargesForCurrentCard); //WC will optimize for effeciency and by default uses a signleton unless we specify the lifecycle. var shopper2 = container.Resolve <IShopper>(); shopper2.Charge(); Console.WriteLine(shopper2.ChargesForCurrentCard); Console.ReadKey(); }
static void Main() { var container = new WindsorContainer(); container.Register(Component.For<IFoo>().ImplementedBy<DefaultFoo>().LifeStyle.Transient); container.Register(Component.For<IBar>().ImplementedBy<DefaultBar>()); Console.WriteLine("using root container"); var foo1 = container.Resolve<IFoo>(); foo1.DoFoo(); var childContainer = new WindsorContainer(); container.AddChildContainer(childContainer); //childContainer.Register(Component.For<IFoo>().ImplementedBy<AlternateFoo>()); childContainer.Register(Component.For<IBar>().ImplementedBy<AlternateBar>()); Console.WriteLine("using child container"); var foo2 = childContainer.Resolve<IFoo>(); foo2.DoFoo(); }
public void ShouldResolveDecoratedComponentFromParent() { var parent = new WindsorContainer(); var child = new WindsorContainer(); parent.AddChildContainer(child); parent.Register( Component.For(typeof(IDoNothingService)).ImplementedBy(typeof(DoNothingServiceDecorator)).Named( "DoNothingServiceDecorator")); parent.Register( Component.For(typeof(IDoNothingService)).ImplementedBy(typeof(DoNothingService)).Named("DoNothingService")); child.Register( Component.For(typeof(IDoSomethingService)).ImplementedBy(typeof(DoSomethingService)).Named("DoSometingService")); var service = child.Resolve<IDoNothingService>(); Assert.IsNotNull(service); Assert.IsInstanceOf(typeof(DoNothingServiceDecorator), service); }