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