public void Instance_per_call_components_should_not_cause_memory_leaks()
        {
            const int iterations = 20000;

            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(typeof(InstancePerCallComponent), DependencyLifecycle.InstancePerUnitOfWork);

                GC.Collect();
                var before = GC.GetTotalMemory(true);
                var sw     = Stopwatch.StartNew();

                for (var i = 0; i < iterations; i++)
                {
                    using (var nestedContainer = builder.BuildChildContainer())
                    {
                        nestedContainer.Build(typeof(InstancePerCallComponent));
                    }
                }

                sw.Stop();
                // Collect all generations of memory.
                GC.Collect();

                var after = GC.GetTotalMemory(true);
                Console.WriteLine("{0} Time: {1} MemDelta: {2} bytes", builder.GetType().Name, sw.Elapsed, after - before);

                var upperLimitBytes = 200 * 1024;
                Assert.That(after - before, Is.LessThan(upperLimitBytes), "Apparently {0} consumed more than {1} KB of memory", builder, upperLimitBytes / 1024);
            }

            //Not supported by, typeof(NinjectObjectBuilder));
        }
        public void Instance_per_uow__components_should_not_be_shared_across_child_containers()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(typeof(InstancePerUoWComponent),
                                  DependencyLifecycle.InstancePerUnitOfWork);

                var task1 = Task <object> .Factory.StartNew(() =>
                {
                    using (var childContainer = builder.BuildChildContainer())
                    {
                        return(childContainer.Build(typeof(InstancePerUoWComponent)));
                    }
                });

                var task2 = Task <object> .Factory.StartNew(() =>
                {
                    using (var childContainer = builder.BuildChildContainer())
                    {
                        return(childContainer.Build(typeof(InstancePerUoWComponent)));
                    }
                });

                Assert.AreNotSame(task1.Result, task2.Result);
            }


            //Not supported bytypeof(SpringObjectBuilder));
        }
 public void Lambda_singlecall_components_should_yield_unique_instances()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         InitializeBuilder(builder);
         Assert.AreNotEqual(builder.Build(typeof(SingleCallLambdaComponent)), builder.Build(typeof(SingleCallLambdaComponent)));
     }
 }
Ejemplo n.º 4
0
 public void Resolving_recursive_types_does_not_stack_overflow()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         InitializeBuilder(builder);
         builder.Build(typeof(RecursiveComponent));
     }
 }
 public void Lambda_singleton_components_should_yield_the_same_instance()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         InitializeBuilder(builder);
         Assert.AreEqual(builder.Build(typeof(SingletonLambdaComponent)), builder.Build(typeof(SingletonLambdaComponent)));
     }
 }
 public void Resolving_all_components_of_unregistered_types_should_give_empty_list()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         InitializeBuilder(builder);
         Assert.IsEmpty(builder.BuildAll(typeof(UnregisteredComponent)));
     }
 }
        public void When_circular_ref_exists_between_container_and_builder_should_not_infinite_loop()
        {
            var builder = TestContainerBuilder.ConstructBuilder();

            Debug.WriteLine("Trying " + builder.GetType().Name);
            builder.RegisterSingleton(builder.GetType(), builder);
            builder.Dispose();
        }
 public void Non_existing_components_should_return_false()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         InitializeBuilder(builder);
         Assert.False(builder.HasComponent(typeof(NonExistingComponent)));
     }
 }
 public void Requesting_an_unregistered_component_should_throw()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         InitializeBuilder(builder);
         Assert.That(() => builder.Build(typeof(UnregisteredComponent)), Throws.Exception);
     }
 }
 public void Builders_should_not_determine_existence_by_building_components()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         InitializeBuilder(builder);
         Assert.True(builder.HasComponent(typeof(ExistingComponentWithUnsatisfiedDependency)));
     }
 }
 public void Lambda_uow_components_should_resolve_from_main_container()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         InitializeBuilder(builder);
         Assert.NotNull(builder.Build(typeof(LambdaComponentUoW)));
     }
     //Not supported by typeof(WindsorObjectBuilder));
 }
Ejemplo n.º 12
0
        public void Concrete_classes_should_get_the_same_lifecycle_as_their_interfaces()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(typeof(SingletonComponent), DependencyLifecycle.SingleInstance);

                Assert.AreSame(builder.Build(typeof(SingletonComponent)), builder.Build(typeof(ISingletonComponent)));
            }
        }
Ejemplo n.º 13
0
        public void Should_support_lambdas_that_uses_other_components_registered_later()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(() => ((StaticFactory)builder.Build(typeof(StaticFactory))).Create(), DependencyLifecycle.InstancePerCall);
                builder.Configure(() => new StaticFactory(), DependencyLifecycle.SingleInstance);

                Assert.NotNull(builder.Build(typeof(ComponentCreatedByFactory)));
            }
        }
Ejemplo n.º 14
0
        public void A_registration_should_update_default_component_for_interface()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(typeof(SomeClass), DependencyLifecycle.InstancePerCall);
                builder.Configure(typeof(SomeOtherClass), DependencyLifecycle.InstancePerCall);

                Assert.IsInstanceOf <SomeOtherClass>(builder.Build(typeof(ISomeInterface)));
            }
        }
Ejemplo n.º 15
0
        public void Generic_interfaces_should_be_registered()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(typeof(ComponentWithGenericInterface),
                                  DependencyLifecycle.InstancePerCall);

                Assert.True(builder.HasComponent(typeof(ISomeGenericInterface <string>)));
            }
        }
        public void A_registration_should_be_allowed_to_be_updated()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.RegisterSingleton(typeof(ISingletonComponent), new SingletonComponent());
                builder.RegisterSingleton(typeof(ISingletonComponent), new AnotherSingletonComponent());

                Assert.IsInstanceOf <AnotherSingletonComponent>(builder.Build(typeof(ISingletonComponent)));
            }
        }
Ejemplo n.º 17
0
        public void Multiple_registrations_of_the_same_component_should_be_allowed()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(typeof(DuplicateClass), DependencyLifecycle.InstancePerCall);
                builder.Configure(typeof(DuplicateClass), DependencyLifecycle.InstancePerCall);

                Assert.AreEqual(1, builder.BuildAll(typeof(DuplicateClass)).Count());
            }
        }
Ejemplo n.º 18
0
        public void System_interfaces_should_not_be_auto_registered()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(typeof(ComponentWithSystemInterface),
                                  DependencyLifecycle.InstancePerCall);

                Assert.False(builder.HasComponent(typeof(IGrouping <string, string>)));
                Assert.False(builder.HasComponent(typeof(IDisposable)));
            }
        }
Ejemplo n.º 19
0
 public void Register_singleton_should_be_supported()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         var singleton = new SingletonComponent();
         builder.RegisterSingleton(typeof(ISingletonComponent), singleton);
         builder.RegisterSingleton(typeof(SingletonComponent), singleton);
         Assert.AreEqual(builder.Build(typeof(SingletonComponent)), singleton);
         Assert.AreEqual(builder.Build(typeof(ISingletonComponent)), singleton);
     }
 }
        public void Singleton_components_should_get_their_dependencies_autowired()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.RegisterSingleton(typeof(ISingletonComponentWithPropertyDependency), new SingletonComponentWithPropertyDependency());
                builder.RegisterSingleton(typeof(SingletonComponent), new SingletonComponent());

                var singleton = (SingletonComponentWithPropertyDependency)builder.Build(typeof(ISingletonComponentWithPropertyDependency));
                Assert.IsNotNull(singleton.Dependency);
            }
        }
        public void Lambda_uow_components_should_yield_the_same_instance()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                InitializeBuilder(builder);

                var instance1 = builder.Build(typeof(LambdaComponentUoW));
                var instance2 = builder.Build(typeof(LambdaComponentUoW));

                Assert.AreSame(instance1, instance2);
            }
        }
Ejemplo n.º 22
0
        public void Properties_configured_multiple_times_should_retain_only_the_last_configuration()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(typeof(DuplicateClass), DependencyLifecycle.SingleInstance);
                builder.ConfigureProperty(typeof(DuplicateClass), "SomeProperty", false);
                builder.ConfigureProperty(typeof(DuplicateClass), "SomeProperty", true); // this should remove/override the previous property setting

                var component = (DuplicateClass)builder.Build(typeof(DuplicateClass));
                Assert.True(component.SomeProperty);
            }
        }
Ejemplo n.º 23
0
        public void Setter_dependencies_should_override_container_defaults()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(typeof(SomeClass), DependencyLifecycle.InstancePerCall);
                builder.Configure(typeof(ClassWithSetterDependencies), DependencyLifecycle.SingleInstance);
                builder.ConfigureProperty(typeof(ClassWithSetterDependencies), "InterfaceDependency", new SomeOtherClass());

                var component = (ClassWithSetterDependencies)builder.Build(typeof(ClassWithSetterDependencies));
                Assert.IsInstanceOf(typeof(SomeOtherClass), component.InterfaceDependency, "Explicitly set dependency should be injected, not container's default type");
            }
        }
        public void All_implemented_interfaces_should_be_registered_for_func()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(() => new ComponentWithMultipleInterfaces(), DependencyLifecycle.InstancePerCall);

                Assert.True(builder.HasComponent(typeof(ISomeInterface)));
                Assert.True(builder.HasComponent(typeof(ISomeOtherInterface)));
                Assert.True(builder.HasComponent(typeof(IYetAnotherInterface)));
                Assert.AreEqual(1, builder.BuildAll(typeof(IYetAnotherInterface)).Count());
            }
        }
        public void Instance_per_uow__components_should_be_disposed_when_the_child_container_is_disposed()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(typeof(InstancePerUoWComponent), DependencyLifecycle.InstancePerUnitOfWork);

                using (var nestedContainer = builder.BuildChildContainer())
                    nestedContainer.Build(typeof(InstancePerUoWComponent));
                Assert.True(InstancePerUoWComponent.DisposeCalled);
            }
            //Not supported bytypeof(SpringObjectBuilder));
        }
        public void Setter_injection_should_be_enabled_by_default()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(typeof(SomeClass), DependencyLifecycle.SingleInstance);
                builder.Configure(typeof(ClassWithSetterDependencies), DependencyLifecycle.SingleInstance);

                var component = (ClassWithSetterDependencies)builder.Build(typeof(ClassWithSetterDependencies));
                Assert.NotNull(component.ConcreteDependency, "Concrete classed should be property injected");
                Assert.NotNull(component.InterfaceDependency, "Interfaces should be property injected");
                Assert.NotNull(component.concreteDependencyWithSetOnly, "Set only properties should be supported");
            }
        }
Ejemplo n.º 27
0
        public void Setter_dependencies_should_be_supported_when_resolving_interfaces()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(typeof(SomeClass), DependencyLifecycle.InstancePerCall);
                builder.RegisterSingleton(typeof(IWithSetterDependencies), new ClassWithSetterDependencies());

                var component = (ClassWithSetterDependencies)builder.Build(typeof(IWithSetterDependencies));
                Assert.NotNull(component.ConcreteDependency, "Concrete classed should be property injected");
                Assert.NotNull(component.InterfaceDependency, "Interfaces should be property injected");
                Assert.NotNull(component.concreteDependencyWithSetOnly, "Set only properties should be supported");
            }
        }
        public void UoW_components_in_the_parent_container_should_be_singletons_in_the_child_container()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(typeof(InstancePerUoWComponent), DependencyLifecycle.InstancePerUnitOfWork);

                using (var nestedContainer = builder.BuildChildContainer())
                {
                    Assert.AreSame(nestedContainer.Build(typeof(InstancePerUoWComponent)), nestedContainer.Build(typeof(InstancePerUoWComponent)), "UoW's should be singleton in child container");
                }
            }
            //Not supported bytypeof(SpringObjectBuilder));
        }
        public void Should_be_able_to_build_components_registered_after_first_build()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                InitializeBuilder(builder);
                builder.Build(typeof(SingletonComponent));
                builder.Configure(typeof(UnregisteredComponent), DependencyLifecycle.SingleInstance);

                var unregisteredComponent = builder.Build(typeof(UnregisteredComponent)) as UnregisteredComponent;
                Assert.NotNull(unregisteredComponent);
                Assert.NotNull(unregisteredComponent.SingletonComponent);
            }
            //Not supported by,typeof(SpringObjectBuilder));
        }
 public void Resolving_recursive_types_does_not_stack_overflow()
 {
     try
     {
         using (var builder = TestContainerBuilder.ConstructBuilder())
         {
             InitializeBuilder(builder);
             builder.Build(typeof(RecursiveComponent));
         }
     }
     catch (Exception)
     {
         // this can't be a StackOverflowException as they can't be caught
     }
 }