Пример #1
0
        public void Resolving_all_components_of_unregistered_types_should_give_empty_list()
        {
            var serviceCollection = new ServiceCollection();

            InitializeServices(serviceCollection);
            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            Assert.IsEmpty(builder.GetServices(typeof(UnregisteredComponent)));
        }
Пример #2
0
        public void Lambda_singleton_components_should_yield_the_same_instance()
        {
            var serviceCollection = new ServiceCollection();

            InitializeServices(serviceCollection);
            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            Assert.AreEqual(builder.GetService(typeof(SingletonLambdaComponent)), builder.GetService(typeof(SingletonLambdaComponent)));
        }
Пример #3
0
        public void Singlecall_components_should_yield_unique_instances()
        {
            var serviceCollection = new ServiceCollection();

            InitializeServices(serviceCollection);
            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            Assert.AreNotEqual(builder.GetService <SinglecallComponent>(), builder.GetService <SinglecallComponent>());
        }
Пример #4
0
        public void Concrete_classes_should_get_the_same_lifecycle_as_their_interfaces()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            configureComponents.ConfigureComponent(typeof(SingletonComponent), DependencyLifecycle.SingleInstance);

            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            Assert.AreSame(builder.GetService(typeof(SingletonComponent)), builder.GetService(typeof(ISingletonComponent)));
        }
Пример #5
0
        public void Multiple_registrations_of_the_same_component_should_be_allowed()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            configureComponents.ConfigureComponent(typeof(DuplicateClass), DependencyLifecycle.InstancePerCall);
            configureComponents.ConfigureComponent(typeof(DuplicateClass), DependencyLifecycle.InstancePerCall);

            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            Assert.AreEqual(1, builder.GetServices(typeof(DuplicateClass)).Count());
        }
Пример #6
0
        public void Should_support_lambdas_that_uses_other_components_registered_later()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            configureComponents.ConfigureComponent(s => ((StaticFactory)s.GetService(typeof(StaticFactory))).Create(), DependencyLifecycle.InstancePerCall);
            configureComponents.ConfigureComponent(() => new StaticFactory(), DependencyLifecycle.SingleInstance);

            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            Assert.NotNull(builder.GetService(typeof(ComponentCreatedByFactory)));
        }
Пример #7
0
        public void Lambda_uow_components_should_yield_the_same_instance()
        {
            var serviceCollection = new ServiceCollection();

            InitializeServices(serviceCollection);
            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

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

            Assert.AreSame(instance1, instance2);
        }
Пример #8
0
        public void A_registration_should_be_allowed_to_be_updated()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            configureComponents.RegisterSingleton(typeof(ISingletonComponent), new SingletonComponent());
            configureComponents.RegisterSingleton(typeof(ISingletonComponent), new AnotherSingletonComponent());

            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            Assert.IsInstanceOf <AnotherSingletonComponent>(builder.GetService(typeof(ISingletonComponent)));
        }
Пример #9
0
        public void All_implemented_interfaces_should_be_registered_for_func()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            configureComponents.ConfigureComponent(() => new ComponentWithMultipleInterfaces(), DependencyLifecycle.InstancePerCall);
            Assert.True(configureComponents.HasComponent(typeof(ISomeInterface)));
            Assert.True(configureComponents.HasComponent(typeof(ISomeOtherInterface)));
            Assert.True(configureComponents.HasComponent(typeof(IYetAnotherInterface)));

            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            Assert.AreEqual(1, builder.GetServices(typeof(IYetAnotherInterface)).Count());
        }
Пример #10
0
 public void Resolving_recursive_types_does_not_stack_overflow()
 {
     try
     {
         var serviceCollection = new ServiceCollection();
         InitializeServices(serviceCollection);
         var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);
         builder.GetService(typeof(RecursiveComponent));
     }
     catch (Exception)
     {
         // this can't be a StackOverflowException as they can't be caught
     }
 }
Пример #11
0
        public void Register_singleton_should_be_supported()
        {
            var singleton           = new SingletonComponent();
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            configureComponents.RegisterSingleton(typeof(ISingletonComponent), singleton);
            configureComponents.RegisterSingleton(typeof(SingletonComponent), singleton);

            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            Assert.AreEqual(builder.GetService(typeof(SingletonComponent)), singleton);
            Assert.AreEqual(builder.GetService(typeof(ISingletonComponent)), singleton);
        }
Пример #12
0
        public void Instance_per_uow__components_should_be_disposed_when_the_child_container_is_disposed()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            configureComponents.ConfigureComponent(typeof(InstancePerUoWComponent), DependencyLifecycle.InstancePerUnitOfWork);

            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            using (var scope = builder.CreateScope())
            {
                scope.ServiceProvider.GetService(typeof(InstancePerUoWComponent));
            }
            Assert.True(InstancePerUoWComponent.DisposeCalled);
        }
Пример #13
0
        public void UoW_components_in_the_parent_container_should_be_singletons_in_the_same_child_container()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            configureComponents.ConfigureComponent(typeof(InstancePerUoWComponent), DependencyLifecycle.InstancePerUnitOfWork);

            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            using (var scope = builder.CreateScope())
            {
                var instance1 = scope.ServiceProvider.GetService(typeof(InstancePerUoWComponent));
                var instance2 = scope.ServiceProvider.GetService(typeof(InstancePerUoWComponent));

                Assert.AreSame(instance1, instance2, "UoW's should be singleton in child container");
            }
        }
Пример #14
0
        public void UoW_components_built_on_root_container_should_be_singletons_even_with_child_builder_present()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            configureComponents.ConfigureComponent(typeof(InstancePerUoWComponent), DependencyLifecycle.InstancePerUnitOfWork);

            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            using (builder.CreateScope())
            {
            }
            var instance1 = builder.GetService(typeof(InstancePerUoWComponent));
            var instance2 = builder.GetService(typeof(InstancePerUoWComponent));

            Assert.AreSame(instance1, instance2, "UoW's should be singletons in the root container");
        }
Пример #15
0
        public void Should_not_dispose_singletons_when_container_goes_out_of_scope()
        {
            var serviceCollection        = new ServiceCollection();
            var configureComponents      = new CommonObjectBuilder(serviceCollection);
            var singletonInMainContainer = new SingletonComponent();

            configureComponents.RegisterSingleton(typeof(ISingletonComponent), singletonInMainContainer);
            configureComponents.ConfigureComponent(typeof(ComponentThatDependsOfSingleton), DependencyLifecycle.InstancePerUnitOfWork);

            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            using (var scope = builder.CreateScope())
            {
                scope.ServiceProvider.GetService(typeof(ComponentThatDependsOfSingleton));
            }
            Assert.False(SingletonComponent.DisposeCalled);
        }
Пример #16
0
        public void Given_lookupType_should_be_used_as_service_in_the_registration_when_RegisterSingleton()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);
            var expected            = new InheritedFromSomeClass();

            configureComponents.RegisterSingleton(typeof(SomeClass), expected);

            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            Assert.AreEqual(expected, builder.GetService(typeof(SomeClass)));

            using (var scope = builder.CreateScope())
            {
                Assert.AreEqual(expected, scope.ServiceProvider.GetService(typeof(SomeClass)));
            }
        }
Пример #17
0
        public void Instance_per_uow_components_should_yield_different_instances_between_parent_and_child_containers()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            configureComponents.ConfigureComponent(typeof(InstancePerUoWComponent), DependencyLifecycle.InstancePerUnitOfWork);

            var builder        = TestContainerBuilder.CreateServiceProvider(serviceCollection);
            var parentInstance = builder.GetService(typeof(InstancePerUoWComponent));

            using (var scope = builder.CreateScope())
            {
                var childInstance = scope.ServiceProvider.GetService(typeof(InstancePerUoWComponent));

                Assert.AreNotSame(parentInstance, childInstance);
            }
        }
Пример #18
0
        public void Registering_the_same_singleton_for_different_interfaces_should_be_supported()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);
            var singleton           = new SingletonThatImplementsToInterfaces();

            configureComponents.RegisterSingleton(typeof(ISingleton1), singleton);
            configureComponents.RegisterSingleton(typeof(ISingleton2), singleton);
            configureComponents.ConfigureComponent(typeof(ComponentThatDependsOnMultiSingletons), DependencyLifecycle.InstancePerCall);

            var builder    = TestContainerBuilder.CreateServiceProvider(serviceCollection);
            var dependency = (ComponentThatDependsOnMultiSingletons)builder.GetService(typeof(ComponentThatDependsOnMultiSingletons));

            Assert.NotNull(dependency.Singleton1);
            Assert.NotNull(dependency.Singleton2);

            Assert.AreEqual(builder.GetService(typeof(ISingleton1)), singleton);
            Assert.AreEqual(builder.GetService(typeof(ISingleton2)), singleton);
        }
Пример #19
0
        public void Multiple_implementations_should_be_supported()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            configureComponents.ConfigureComponent(typeof(SomeClass), DependencyLifecycle.InstancePerUnitOfWork);
            configureComponents.ConfigureComponent(typeof(SomeOtherClass), DependencyLifecycle.InstancePerUnitOfWork);

            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            Assert.NotNull(builder.GetService(typeof(SomeClass)));
            Assert.AreEqual(2, builder.GetServices(typeof(ISomeInterface)).Count());

            using (var scope = builder.CreateScope())
            {
                Assert.NotNull(scope.ServiceProvider.GetService(typeof(SomeClass)));
                Assert.AreEqual(2, scope.ServiceProvider.GetServices(typeof(ISomeInterface)).Count());
            }
        }
Пример #20
0
        public void Should_dispose_all_non_percall_IDisposable_components_in_child_container()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            DisposableComponent.DisposeCalled        = false;
            AnotherDisposableComponent.DisposeCalled = false;
            configureComponents.RegisterSingleton(typeof(AnotherDisposableComponent), new AnotherDisposableComponent());
            configureComponents.ConfigureComponent(typeof(DisposableComponent), DependencyLifecycle.InstancePerUnitOfWork);


            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            using (var scope = builder.CreateScope())
            {
                scope.ServiceProvider.GetService(typeof(DisposableComponent));
            }
            Assert.False(AnotherDisposableComponent.DisposeCalled, "Dispose should not be called on AnotherSingletonComponent because it belongs to main container");
            Assert.True(DisposableComponent.DisposeCalled, "Dispose should be called on DisposableComponent");
        }
Пример #21
0
        public void Should_dispose_all_IDisposable_components()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            DisposableComponent.DisposeCalled       = false;
            AnotherSingletonComponent.DisposeCalled = false;

            configureComponents.ConfigureComponent(typeof(DisposableComponent), DependencyLifecycle.SingleInstance);
            configureComponents.RegisterSingleton(typeof(AnotherSingletonComponent), new AnotherSingletonComponent());

            var builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);

            builder.GetService(typeof(DisposableComponent));
            builder.GetService(typeof(AnotherSingletonComponent));
            (builder as IDisposable)?.Dispose();

            Assert.True(DisposableComponent.DisposeCalled, "Dispose should be called on DisposableComponent");
            Assert.True(AnotherSingletonComponent.DisposeCalled, "Dispose should be called on AnotherSingletonComponent");
        }
Пример #22
0
        public void Instance_per_call_components_should_not_be_shared_across_child_containers()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            configureComponents.ConfigureComponent(typeof(InstancePerCallComponent), DependencyLifecycle.InstancePerCall);

            var    builder = TestContainerBuilder.CreateServiceProvider(serviceCollection);
            object instance1;

            using (var scope = builder.CreateScope())
            {
                instance1 = scope.ServiceProvider.GetService(typeof(InstancePerCallComponent));
            }

            object instance2;

            using (var scope = builder.CreateScope())
            {
                instance2 = scope.ServiceProvider.GetService(typeof(InstancePerCallComponent));
            }

            Assert.AreNotSame(instance1, instance2);
        }