/// <summary> /// Creates Simplify.DI wrapper service provider for Microsoft.Extensions.DependencyInjection /// </summary> /// <param name="provider">The resolver.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">collection</exception> public static IServiceProvider CreateServiceProvider(this IDIContainerProvider provider) { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } var serviceProvider = new DIServiceProvider(provider); // Registering required IOC container types wrappers in itself provider.Register <IServiceProvider>(x => serviceProvider, LifetimeType.Singleton); provider.Register <IServiceScopeFactory>(x => new DIServiceScopeFactory(provider), LifetimeType.Singleton); return(serviceProvider); }
public void ScopedResolve_ScopeRegistered_Resolved() { // Assign _provider.Register <NonDepFoo>(); NonDepFoo foo; // Act using (var scope = _provider.BeginLifetimeScope()) foo = scope.Resolver.Resolve <NonDepFoo>(); // Assert Assert.IsNotNull(foo); }
public void Resolve_ScopeRegisteredAndRequestedOutsideOfTheScope_ActivationException() { // Assign _provider.Register <NonDepFoo>(); // Act & Assert var ex = Assert.Throws <ActivationException>(() => _provider.Resolve <NonDepFoo>()); Assert.That(ex.Message, Does.StartWith("NonDepFoo is registered as 'Async Scoped' lifestyle, but the instance is requested outside the context of an active (Async Scoped) scope.")); }
public void Resolve_ScopeRegisteredAndRequestedOutsideOfTheScope_ContainerException() { // Assign _provider.Register <NonDepFoo>(); // Act & Assert var ex = Assert.Throws <ContainerException>(() => _provider.Resolve <NonDepFoo>()); Assert.That(ex.Message, Does.StartWith("No current scope is available: probably you are registering to, or resolving from outside of the scope.")); }
public void Resolve_AllSingletones_EqualObjects() { // Assign _provider.Register <Foo>(LifetimeType.Singleton); _provider.Register <Bar1>(LifetimeType.Singleton); _provider.Register <Bar2>(LifetimeType.Singleton); // Act var foo = _provider.Resolve <Foo>(); var foo2 = _provider.Resolve <Foo>(); Foo foo3; Foo foo4; using (var scope = _provider.BeginLifetimeScope()) foo3 = scope.Container.Resolve <Foo>(); using (var scope = _provider.BeginLifetimeScope()) foo4 = scope.Container.Resolve <Foo>(); // Assert Assert.AreEqual(foo, foo2); Assert.AreEqual(foo, foo3); Assert.AreEqual(foo, foo4); }
/// <summary> /// Registers the specified concrete type for resolve. /// </summary> /// <typeparam name="TConcrete">Concrete type.</typeparam> /// <param name="provider">The DI provider.</param> /// <param name="lifetimeType">Lifetime type of the registering concrete type.</param> public static void Register <TConcrete>(this IDIContainerProvider provider, LifetimeType lifetimeType = LifetimeType.Singleton) where TConcrete : class { provider.Register(typeof(TConcrete), typeof(TConcrete), lifetimeType); }
/// <summary> /// Registers the specified service type with corresponding implementation type. /// </summary> /// <typeparam name="TService">Service type.</typeparam> /// <typeparam name="TImplementation">Implementation type.</typeparam> /// <param name="provider">The DI provider.</param> /// <param name="lifetimeType">Lifetime type of the registering service type.</param> public static void Register <TService, TImplementation>(this IDIContainerProvider provider, LifetimeType lifetimeType = LifetimeType.Singleton) { provider.Register(typeof(TService), typeof(TImplementation), lifetimeType); }
/// <summary> /// Registers the specified concrete type for resolve. /// </summary> /// <param name="provider">The DI provider.</param> /// <param name="concreteType">Concrete type.</param> /// <param name="lifetimeType">Lifetime type of the registering concrete type.</param> public static void Register(this IDIContainerProvider provider, Type concreteType, LifetimeType lifetimeType = LifetimeType.Singleton) { provider.Register(concreteType, concreteType, lifetimeType); }
public void Resolve_ScopeRegisteredAndRequestedOutsideOfTheScope_InvalidOperationException() { // Assign _provider.Register <NonDepFoo>(); // Act & Assert var ex = Assert.Throws <InvalidOperationException>(() => _provider.Resolve <NonDepFoo>()); Assert.That(ex.Message, Does.StartWith("Scope was not available. Did you forget to call container.BeginScope()?")); }
/// <summary> /// Registers the specified service type with corresponding implementation type. /// </summary> /// <typeparam name="TService">Service type.</typeparam> /// <param name="provider">The DI provider.</param> /// <param name="implementationType">Implementation type.</param> /// <param name="lifetimeType">Lifetime type of the registering service type.</param> public static void Register <TService>(this IDIContainerProvider provider, Type implementationType, LifetimeType lifetimeType = LifetimeType.PerLifetimeScope) { provider.Register(typeof(TService), implementationType, lifetimeType); }
public void Resolve_ScopeRegisteredAndRequestedOutsideOfTheScope_InvalidOperationException() { // Assign _provider.Register <NonDepFoo>(); // Act & Assert var ex = Assert.Throws <InvalidOperationException>(() => _provider.Resolve <NonDepFoo>()); Assert.That(ex.Message, Does.StartWith("Cannot resolve scoped service 'Simplify.DI.TestsTypes.NonDepFoo' from root provider.")); }
public void Resolve_ScopeRegisteredAndRequestedOutsideOfTheScope_ContainerException() { // Assign _provider.Register <NonDepFoo>(); // Act & Assert var ex = Assert.Throws <ContainerException>(() => _provider.Resolve <NonDepFoo>()); Assert.That(ex.Message, Does.StartWith("code: Error.NoCurrentScope")); }
public static IDIContainerProvider RegisterAll(this IDIContainerProvider provider) { provider.Register <Dependency>(); return(provider); }