public IScopeFactory CreateBuilder(IServiceCollection services) { bootstrapper.AddIoCRegistration(new TypeRegistration <CQELightServiceProvider>(true)); bootstrapper.AddIoCRegistration(new TypeRegistration <CQELightServiceScopeFactory>(true)); foreach (var item in services) { if (item.ServiceType != null) { if (item.ImplementationType != null) { bootstrapper.AddIoCRegistration(new TypeRegistration(item.ImplementationType, item.Lifetime == ServiceLifetime.Singleton ? RegistrationLifetime.Singleton : RegistrationLifetime.Transient, TypeResolutionMode.OnlyUsePublicCtors, item.ServiceType)); } else if (item.ImplementationFactory != null) { bootstrapper.AddIoCRegistration(new FactoryRegistration(() => item.ImplementationFactory( new CQELightServiceProvider(DIManager.BeginScope().Resolve <IScopeFactory>())), item.ServiceType)); } else if (item.ImplementationInstance != null) { bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(item.ImplementationInstance, item.ServiceType)); } } } bootstrapper.Bootstrapp(); return(DIManager._scopeFactory); }
public void Factory_Registration_With_Scope_Should_Not_Disposed() { var b = new Bootstrapper(); b.AddIoCRegistration(new TypeRegistration(typeof(FactoryDisposable), true)); b.AddIoCRegistration(new FactoryRegistration(s => new FactoryObject(s.Resolve <FactoryDisposable>()), typeof(IFactoryObject))); b.UseMicrosoftDependencyInjection(new ServiceCollection()); b.Bootstrapp(); var scope = DIManager.BeginScope(); var obj = scope.Resolve <IFactoryObject>(); obj.Obj.Should().NotBeNull(); obj.Obj.IsDisposed.Should().BeFalse(); }
public void Bootstrapper_AddIoCRegistration_TestParams() { Assert.Throws <ArgumentNullException>(() => new Bootstrapper().AddIoCRegistration(null)); var b = new Bootstrapper(); b.AddIoCRegistration(new TypeRegistration(typeof(DateTime), typeof(DateTime))); b.IoCRegistrations.Should().HaveCount(1); b.IoCRegistrations.First().Should().BeOfType <TypeRegistration>(); }
public IScopeFactory CreateBuilder(IServiceCollection services) { RegistrationLifetime GetLifetimeFromServiceLifetime(ServiceLifetime lifetime) => lifetime switch { ServiceLifetime.Singleton => RegistrationLifetime.Singleton, ServiceLifetime.Scoped => RegistrationLifetime.Scoped, _ => RegistrationLifetime.Transient }; bootstrapper.AddIoCRegistration(new TypeRegistration <CQELightServiceProvider>(true)); bootstrapper.AddIoCRegistration(new TypeRegistration <CQELightServiceScopeFactory>(true)); bootstrapper.AddIoCRegistration(new TypeRegistration <CQELightServiceProvider>(typeof(IServiceProvider), typeof(ISupportRequiredService))); bootstrapper.AddIoCRegistration(new TypeRegistration <CQELightServiceProviderFactory>(typeof(IServiceProviderFactory <IScopeFactory>))); bootstrapper.AddIoCRegistration(new TypeRegistration <CQELightServiceScope>(typeof(IServiceScope))); foreach (var item in services) { if (item.ServiceType != null) { if (item.ImplementationType != null) { bootstrapper.AddIoCRegistration(new TypeRegistration( item.ImplementationType, GetLifetimeFromServiceLifetime(item.Lifetime), TypeResolutionMode.OnlyUsePublicCtors, item.ServiceType)); } else if (item.ImplementationFactory != null) { bootstrapper.AddIoCRegistration(new FactoryRegistration(s => item.ImplementationFactory( new CQELightServiceProvider(s.ResolveRequired <IScopeFactory>().CreateScope())), GetLifetimeFromServiceLifetime(item.Lifetime), item.ServiceType)); } else if (item.ImplementationInstance != null) { bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(item.ImplementationInstance, GetLifetimeFromServiceLifetime(item.Lifetime), item.ServiceType)); } } } if (!bootstrapper.RegisteredServices.Any(s => s.ServiceType == BootstrapperServiceType.IoC)) { bootstrapper.UseMicrosoftDependencyInjection(services); } bootstrapper.Bootstrapp(); return(DIManager._scopeFactory !); }
public void Bootstrapper_Should_Not_Register_AlreadyExistingServices() { var services = new ServiceCollection(); services.AddScoped <IScopeTest, ScopeTest>(); services.AddScoped <ScopeTest>(); var b = new Bootstrapper(); b.AddIoCRegistration(new TypeRegistration(typeof(ScopeTest), true)); b.UseMicrosoftDependencyInjection(services).Bootstrapp(); using (var scope = DIManager.BeginScope()) { var s = scope.Resolve <IScopeTest>(); // will throw exception if registered twice s.Should().NotBeNull(); } }
public void Resolve_TypeParameter_Should_Throw_NotSupported() { var builder = new ServiceCollection(); bootstrapper.AddIoCRegistration(new TypeRegistration(typeof(ParameterResolving), typeof(ParameterResolving))); Bootstrapp(builder); using (var s = DIManager.BeginScope()) { Assert.Throws <NotSupportedException>(() => s.Resolve <IParameterResolving>(new TypeResolverParameter(typeof(string), "test"))); } }
public void Bootstrapper_Bootstrapp_IoCRegistrations_Tests() { var bootstrapperStrict = new Bootstrapper(strict: true); bootstrapperStrict.AddIoCRegistration(new TypeRegistration(typeof(object), typeof(object))); Assert.Throws <InvalidOperationException>(() => bootstrapperStrict.Bootstrapp()); var bootstrapperLazy = new Bootstrapper(); bootstrapperLazy.AddIoCRegistration(new TypeRegistration(typeof(object), typeof(object))); var notifs = bootstrapperLazy.Bootstrapp().ToList(); notifs.Should().HaveCount(1); notifs[0].Type.Should().Be(BootstrapperNotificationType.Error); notifs[0].ContentType.Should().Be(BootstapperNotificationContentType.IoCRegistrationsHasBeenMadeButNoIoCService); }
public void Bootstrapp_Should_Returns_CustomNotification_To_System_Ones() { var bootstrapper = new Bootstrapper(); bootstrapper.AddIoCRegistration(new TypeRegistration(typeof(object), typeof(object))); bootstrapper.AddNotification(new BootstrapperNotification(BootstrapperNotificationType.Error, "error message")); var notifs = bootstrapper.Bootstrapp().OrderBy(n => n.ContentType).ToList(); notifs.Should().HaveCount(2); notifs[0].Type.Should().Be(BootstrapperNotificationType.Error); notifs[0].ContentType.Should().Be(BootstapperNotificationContentType.IoCRegistrationsHasBeenMadeButNoIoCService); notifs[0].Message.Should().BeNullOrWhiteSpace(); notifs[1].Type.Should().Be(BootstrapperNotificationType.Error); notifs[1].ContentType.Should().Be(BootstapperNotificationContentType.CustomServiceNotification); notifs[1].Message.Should().Be("error message"); }
public void Bootstrapper_Bootstrapp_BootstrappingContext_CheckIoCRegistrations() { BootstrappingContext bootstrappContext = null; var iocServiceMock = new Mock <IBootstrapperService>(); iocServiceMock .Setup(m => m.ServiceType).Returns(BootstrapperServiceType.IoC); iocServiceMock .Setup(m => m.BootstrappAction) .Returns((c) => bootstrappContext = c); var b = new Bootstrapper(); b.AddService(iocServiceMock.Object); b.AddIoCRegistration(new TypeRegistration(typeof(object), typeof(object), typeof(DateTime))); b.Bootstrapp(); bootstrappContext.Should().NotBeNull(); bootstrappContext.IsAbstractionRegisteredInIoC(typeof(object)).Should().BeTrue(); bootstrappContext.IsAbstractionRegisteredInIoC(typeof(DateTime)).Should().BeTrue(); }