예제 #1
0
        public void Builders_should_not_determine_existence_by_building_components()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            InitializeBuilder(configureComponents);

            Assert.True(configureComponents.HasComponent(typeof(ExistingComponentWithUnsatisfiedDependency)));
        }
예제 #2
0
        public void Non_existing_components_should_return_false()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

            InitializeBuilder(configureComponents);

            Assert.False(configureComponents.HasComponent(typeof(NonExistingComponent)));
        }
예제 #3
0
        public void Generic_interfaces_should_be_registered()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

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

            Assert.True(configureComponents.HasComponent(typeof(ISomeGenericInterface <string>)));
        }
예제 #4
0
        public void System_interfaces_should_not_be_auto_registered()
        {
            var serviceCollection   = new ServiceCollection();
            var configureComponents = new CommonObjectBuilder(serviceCollection);

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

            Assert.False(configureComponents.HasComponent(typeof(IGrouping <string, string>)));
            Assert.False(configureComponents.HasComponent(typeof(IDisposable)));
        }
예제 #5
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)));
        }
예제 #6
0
        /// <summary>
        /// Sets the Builder property of the given Configure object to an instance of CommonObjectBuilder.
        /// Then, the given builder object is inserted in the relevant place of the builder chain.
        /// Finally, the given actions are performed on the instance of CommonObjectBuilder.
        /// </summary>
        /// <param name="config"></param>
        /// <param name="container"></param>
        public static void With(Configure config, IContainer container)
        {
            var b = new CommonObjectBuilder { Container = container, Synchronized = SyncConfig.Synchronize };

            config.Builder = b;
            config.Configurer = b;

            var cfg = config.Configurer.ConfigureComponent<CommonObjectBuilder>(ComponentCallModelEnum.Singleton)
                .ConfigureProperty(c => c.Container, container);

            SyncConfig.MarkConfigured();
        }
예제 #7
0
        void InitializeServices(IServiceCollection serviceCollection)
        {
            var container = new CommonObjectBuilder(serviceCollection);

            container.ConfigureComponent(typeof(SingletonComponent), DependencyLifecycle.SingleInstance);
            container.ConfigureComponent(typeof(SinglecallComponent), DependencyLifecycle.InstancePerCall);
            container.ConfigureComponent(typeof(InstancePerUoWComponent), DependencyLifecycle.InstancePerUnitOfWork);
            container.ConfigureComponent(() => new SingletonLambdaComponent(), DependencyLifecycle.SingleInstance);
            container.ConfigureComponent(() => new SingleCallLambdaComponent(), DependencyLifecycle.InstancePerCall);
            container.ConfigureComponent(() => new LambdaComponentUoW(), DependencyLifecycle.InstancePerUnitOfWork);
            container.ConfigureComponent(() => new RecursiveComponent(), DependencyLifecycle.SingleInstance);
        }
예제 #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 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)));
        }
예제 #10
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());
        }
예제 #11
0
        /// <summary>
        /// Sets the Builder property of the given Configure object to an instance of CommonObjectBuilder.
        /// Then, the given builder object is inserted in the relevant place of the builder chain.
        /// Finally, the given actions are performed on the instance of CommonObjectBuilder.
        /// </summary>
        public static void With(Configure config, IContainer container)
        {
            var b = new CommonObjectBuilder { Container = container, Synchronized = SyncConfig.Synchronize };

            config.Builder = b;
            config.Configurer = b;

            var cfg = config.Configurer.ConfigureComponent<CommonObjectBuilder>(DependencyLifecycle.SingleInstance)
                .ConfigureProperty(c => c.Container, container);

            SyncConfig.MarkConfigured();
        }
예제 #12
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());
        }
예제 #13
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);
        }
예제 #14
0
        /// <summary>
        /// Sets the Builder property of the given Configure object to an instance of CommonObjectBuilder.
        /// Then, the given builder object is inserted in the relevant place of the builder chain.
        /// Finally, the given actions are performed on the instance of CommonObjectBuilder.
        /// </summary>
        public static void With(Configure config, IContainer container)
        {
            var b = new CommonObjectBuilder {
                Container = container, Synchronized = SyncConfig.Synchronize
            };

            config.Builder    = b;
            config.Configurer = b;

            var cfg = config.Configurer.ConfigureComponent <CommonObjectBuilder>(DependencyLifecycle.SingleInstance)
                      .ConfigureProperty(c => c.Container, container);

            SyncConfig.MarkConfigured();
        }
예제 #15
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);
        }
예제 #16
0
        public void Should_have_configured_maxsize()
        {
            var feature   = new InMemoryGatewayPersistence();
            var settings  = new SettingsHolder();
            var container = new CommonObjectBuilder(new LightInjectObjectBuilder());

            var persistenceSettings = new PersistenceExtensions <InMemoryPersistence>(settings);

            persistenceSettings.GatewayDeduplicationCacheSize(42);

            feature.Setup(new FeatureConfigurationContext(settings, container, null, null, null));

            var implementation = (InMemoryGatewayDeduplication)container.Build <IDeduplicateMessages>();

            Assert.AreEqual(42, implementation.maxSize);
        }
예제 #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 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)));
            }
        }
예제 #19
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");
            }
        }
예제 #20
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);
        }
예제 #21
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");
        }
예제 #22
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);
        }
예제 #23
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());
            }
        }
    void Awake()
    {
        if (commonObjectRoot == null)
        {
            commonObjectRoot = gameObject.GetComponent <CommonObjectBuilder> ();
        }
        else
        {
            Debug.LogError("More than one active CommonObjectRoot");
        }

        Canvas canvasPrefab = Resources.Load("Containers/GuiCanvas", typeof(Canvas)) as Canvas;

        canvas = Instantiate(canvasPrefab, this.gameObject.transform);

        BuildCamera();
        BuildLight();
        BuildGui();
        BuildGame();
    }
예제 #25
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");
        }
예제 #26
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");
        }
예제 #27
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);
        }
        void RegisterContainerAdapter(IContainer containerToAdapt)
        {
            var b = new CommonObjectBuilder(containerToAdapt);

            builder = b;
            container = b;

            container.ConfigureComponent<IBuilder>(_ => b, DependencyLifecycle.SingleInstance);
        }