public async Task PoolsInSingleton() { ServiceContainer services = new ServiceContainer(); services.AddSingleton <IParentService, ParentService>(); services.AddTransientPool <IFirstChildService, FirstChildService>(); services.AddScopedPool <ISecondChildService, SecondChildService>(); IContainerScope scope = services.CreateScope(); await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <IParentService>()); await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <ISecondChildService>()); IParentService parent = await services.Get <IParentService>(scope); parent.Foo = "parent"; parent.First.Foo = "first"; parent.Second.Foo = "second"; //getting same parent instance (cuz of singleton) so, transients in the same parent instances should be same IParentService p1 = await services.Get <IParentService>(scope); Assert.Equal(parent.Foo, p1.Foo); Assert.Equal(parent.First.Foo, p1.First.Foo); Assert.Equal(parent.Second.Foo, p1.Second.Foo); IFirstChildService first = await services.Get <IFirstChildService>(scope); IFirstChildService f2 = await services.Get <IFirstChildService>(); Assert.NotEqual(parent.First.Foo, first.Foo); Assert.NotEqual(parent.First.Foo, f2.Foo); ISecondChildService second = await services.Get <ISecondChildService>(scope); Assert.Equal(parent.Second.Foo, second.Foo); //children in same singleton instance are same IContainerScope scope2 = services.CreateScope(); IParentService p2 = await services.Get <IParentService>(scope2); Assert.Equal(parent.Foo, p2.Foo); Assert.Equal(parent.First.Foo, p2.First.Foo); Assert.Equal(parent.Second.Foo, p2.Second.Foo); }
public async Task PoolsInTransientPool() { ServiceContainer services = new ServiceContainer(); services.AddTransientPool <IParentService, ParentService>(); services.AddTransientPool <IFirstChildService, FirstChildService>(); services.AddScopedPool <ISecondChildService, SecondChildService>(); IContainerScope scope = services.CreateScope(); await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <IParentService>()); await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <ISecondChildService>()); IParentService parent = await services.Get <IParentService>(scope); parent.Foo = "parent"; parent.First.Foo = "first"; parent.Second.Foo = "second"; IParentService p1 = await services.Get <IParentService>(scope); Assert.NotEqual(parent.Foo, p1.Foo); Assert.NotEqual(parent.First.Foo, p1.First.Foo); Assert.Equal(parent.Second.Foo, p1.Second.Foo); IFirstChildService first = await services.Get <IFirstChildService>(scope); IFirstChildService f2 = await services.Get <IFirstChildService>(); Assert.NotEqual(parent.First.Foo, first.Foo); Assert.NotEqual(parent.First.Foo, f2.Foo); ISecondChildService second = await services.Get <ISecondChildService>(scope); Assert.Equal(parent.Second.Foo, second.Foo); IContainerScope scope2 = services.CreateScope(); IParentService p2 = await services.Get <IParentService>(scope2); Assert.NotEqual(parent.Foo, p2.Foo); Assert.NotEqual(parent.First.Foo, p2.First.Foo); Assert.NotEqual(parent.Second.Foo, p2.Second.Foo); }
public void PoolsInScoped() { ServiceContainer services = new ServiceContainer(); services.AddScoped <IParentService, ParentService>(); services.AddTransientPool <IFirstChildService, FirstChildService>(); services.AddScopedPool <ISecondChildService, SecondChildService>(); IContainerScope scope = services.CreateScope(); Assert.Throws <ScopeException>(() => services.Get <IParentService>()); Assert.Throws <ScopeException>(() => services.Get <ISecondChildService>()); IParentService parent = services.Get <IParentService>(scope); parent.Foo = "parent"; parent.First.Foo = "first"; parent.Second.Foo = "second"; //getting same parent instance (cuz of scoped) so, transients in the same parent instances should be same IParentService p1 = services.Get <IParentService>(scope); Assert.Equal(parent.Foo, p1.Foo); Assert.Equal(parent.First.Foo, p1.First.Foo); Assert.Equal(parent.Second.Foo, p1.Second.Foo); IFirstChildService first = services.Get <IFirstChildService>(scope); IFirstChildService f2 = services.Get <IFirstChildService>(); Assert.NotEqual(parent.First.Foo, first.Foo); Assert.NotEqual(parent.First.Foo, f2.Foo); ISecondChildService second = services.Get <ISecondChildService>(scope); Assert.Equal(parent.Second.Foo, second.Foo); IContainerScope scope2 = services.CreateScope(); IParentService p2 = services.Get <IParentService>(scope2); Assert.NotEqual(parent.Foo, p2.Foo); Assert.NotEqual(parent.First.Foo, p2.First.Foo); Assert.NotEqual(parent.Second.Foo, p2.Second.Foo); }
public async Task TransientInScopedPool() { ServiceContainer services = new ServiceContainer(); services.AddScopedPool <IParentService, ParentService>(); services.AddTransient <IFirstChildService, FirstChildService>(); services.AddTransient <ISecondChildService, SecondChildService>(); IContainerScope scope = services.CreateScope(); IParentService parent = await services.Get <IParentService>(scope); parent.Foo = "parent"; parent.First.Foo = "first"; parent.Second.Foo = "second"; //we are getting same instance of parent. so, children are same. IParentService p = await services.Get <IParentService>(scope); Assert.Equal(parent.Foo, p.Foo); Assert.Equal(parent.First.Foo, p.First.Foo); Assert.Equal(parent.Second.Foo, p.Second.Foo); //we are getting individual children, so they are created new and different IFirstChildService f1 = await services.Get <IFirstChildService>(); IFirstChildService f2 = await services.Get <IFirstChildService>(scope); Assert.NotEqual(parent.First.Foo, f1.Foo); Assert.NotEqual(parent.First.Foo, f2.Foo); //we are getting individual children, so they are created new and different ISecondChildService s1 = await services.Get <ISecondChildService>(); ISecondChildService s2 = await services.Get <ISecondChildService>(scope); Assert.NotEqual(parent.Second.Foo, s1.Foo); Assert.NotEqual(parent.Second.Foo, s2.Foo); IContainerScope scope2 = services.CreateScope(); IParentService p2 = await services.Get <IParentService>(scope2); Assert.NotEqual(parent.Foo, p2.Foo); }
public void PoolsInTransientPool() { ServiceContainer services = new ServiceContainer(); services.AddTransientPool <IParentService, ParentService>(); services.AddTransientPool <IFirstChildService, FirstChildService>(); services.AddScopedPool <ISecondChildService, SecondChildService>(); IContainerScope scope = services.CreateScope(); Assert.Throws <ScopeException>(() => services.Get <IParentService>()); Assert.Throws <ScopeException>(() => services.Get <ISecondChildService>()); IParentService parent = services.Get <IParentService>(scope); parent.Foo = "parent"; parent.First.Foo = "first"; parent.Second.Foo = "second"; IParentService p1 = services.Get <IParentService>(scope); Assert.NotEqual(parent.Foo, p1.Foo); Assert.NotEqual(parent.First.Foo, p1.First.Foo); Assert.Equal(parent.Second.Foo, p1.Second.Foo); IFirstChildService first = services.Get <IFirstChildService>(scope); IFirstChildService f2 = services.Get <IFirstChildService>(); Assert.NotEqual(parent.First.Foo, first.Foo); Assert.NotEqual(parent.First.Foo, f2.Foo); ISecondChildService second = services.Get <ISecondChildService>(scope); Assert.Equal(parent.Second.Foo, second.Foo); IContainerScope scope2 = services.CreateScope(); IParentService p2 = services.Get <IParentService>(scope2); Assert.NotEqual(parent.Foo, p2.Foo); Assert.NotEqual(parent.First.Foo, p2.First.Foo); Assert.NotEqual(parent.Second.Foo, p2.Second.Foo); }
public async Task SingletonInScopedPool() { ServiceContainer services = new ServiceContainer(); services.AddScopedPool <IParentService, ParentService>(); services.AddSingleton <IFirstChildService, FirstChildService>(); services.AddSingleton <ISecondChildService, SecondChildService>(); IContainerScope scope = services.CreateScope(); IParentService parent = await services.Get <IParentService>(scope); parent.Foo = "parent"; parent.First.Foo = "first"; parent.Second.Foo = "second"; IParentService p = await services.Get <IParentService>(scope); Assert.Equal(parent.Foo, p.Foo); Assert.Equal(parent.First.Foo, p.First.Foo); Assert.Equal(parent.Second.Foo, p.Second.Foo); IFirstChildService f2 = await services.Get <IFirstChildService>(scope); IFirstChildService f3 = await services.Get <IFirstChildService>(); Assert.Equal(parent.First.Foo, f2.Foo); Assert.Equal(parent.First.Foo, f3.Foo); ISecondChildService s2 = await services.Get <ISecondChildService>(scope); ISecondChildService s3 = await services.Get <ISecondChildService>(); Assert.Equal(parent.Second.Foo, s2.Foo); Assert.Equal(parent.Second.Foo, s3.Foo); IContainerScope scope2 = services.CreateScope(); IParentService p2 = await services.Get <IParentService>(scope2); Assert.NotEqual(parent.Foo, p2.Foo); Assert.Equal(parent.First.Foo, p2.First.Foo); Assert.Equal(parent.Second.Foo, p2.Second.Foo); }
public void Scoped() { ServiceContainer services = new ServiceContainer(); services.AddScopedPool <ISingleService, SingleService>(); IContainerScope scope = services.CreateScope(); ISingleService single = services.Get <ISingleService>(scope); single.Foo = "single"; ISingleService s1 = services.Get <ISingleService>(scope); Assert.Equal(single.Foo, s1.Foo); Assert.Throws <ScopeException>(() => services.Get <ISingleService>()); IContainerScope scope2 = services.CreateScope(); ISingleService s3 = services.Get <ISingleService>(scope2); Assert.NotEqual(single.Foo, s3.Foo); }
public async Task Scoped() { ServiceContainer services = new ServiceContainer(); services.AddScopedPool <ISingleService, SingleService>(); IContainerScope scope = services.CreateScope(); ISingleService single = await services.Get <ISingleService>(scope); single.Foo = "single"; ISingleService s1 = await services.Get <ISingleService>(scope); Assert.Equal(single.Foo, s1.Foo); await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <ISingleService>()); IContainerScope scope2 = services.CreateScope(); ISingleService s3 = await services.Get <ISingleService>(scope2); Assert.NotEqual(single.Foo, s3.Foo); }
public async Task LongRunning() { ServiceContainer services = new ServiceContainer(); services.AddScopedPool <ISingleService, SingleService>(); for (int i = 0; i < 50; i++) { IContainerScope scope1 = services.CreateScope(); IContainerScope scope2 = services.CreateScope(); ISingleService service1 = services.Get <ISingleService>(scope1); ISingleService service2 = services.Get <ISingleService>(scope2); Assert.NotNull(service1); Assert.NotNull(service2); Assert.NotEqual(service1, service2); await Task.Delay(10); scope1.Dispose(); scope2.Dispose(); } }