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))); }
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))); }
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>))); }
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))); }
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))); }
/// <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(); }
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); }
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))); }
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))); }
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()); }
/// <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(); }
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()); }
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); }
/// <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(); }
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); }
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); }
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); } }
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))); } }
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"); } }
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); }
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"); }
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); }
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(); }
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"); }
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"); }
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); }