public void ScopedInTransientPool() { ServiceContainer services = new ServiceContainer(); services.AddTransientPool <IParentService, ParentService>(); services.AddScoped <IFirstChildService, FirstChildService>(); services.AddScoped <ISecondChildService, SecondChildService>(); IContainerScope scope = services.CreateScope(); IParentService parent = services.Get <IParentService>(scope); parent.Foo = "parent"; parent.First.Foo = "first"; parent.Second.Foo = "second"; IParentService p = services.Get <IParentService>(scope); Assert.NotEqual(parent.Foo, p.Foo); Assert.Equal(parent.First.Foo, p.First.Foo); Assert.Equal(parent.Second.Foo, p.Second.Foo); Assert.Throws <ScopeException>(() => services.Get <IFirstChildService>()); IFirstChildService f2 = services.Get <IFirstChildService>(scope); Assert.Equal(parent.First.Foo, f2.Foo); Assert.Throws <ScopeException>(() => services.Get <ISecondChildService>()); ISecondChildService s2 = services.Get <ISecondChildService>(scope); Assert.Equal(parent.Second.Foo, s2.Foo); }
public async Task ScopedInTransientPool() { ServiceContainer services = new ServiceContainer(); services.AddTransientPool <IParentService, ParentService>(); services.AddScoped <IFirstChildService, FirstChildService>(); services.AddScoped <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.NotEqual(parent.Foo, p.Foo); Assert.Equal(parent.First.Foo, p.First.Foo); Assert.Equal(parent.Second.Foo, p.Second.Foo); await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <IFirstChildService>()); IFirstChildService f2 = await services.Get <IFirstChildService>(scope); Assert.Equal(parent.First.Foo, f2.Foo); await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <ISecondChildService>()); ISecondChildService s2 = await services.Get <ISecondChildService>(scope); Assert.Equal(parent.Second.Foo, s2.Foo); }
public void MultipleNestedDoubleParameter() { ServiceContainer services = new ServiceContainer(); services.AddScoped <INestParentService, NestParentService>(); services.AddScoped <ISingleService, SingleService>(); services.AddScoped <IParentService, ParentService>(); services.AddScoped <IFirstChildService, FirstChildService>(); services.AddScoped <ISecondChildService, SecondChildService>(); IContainerScope scope = services.CreateScope(); INestParentService nest = services.Get <INestParentService>(scope); nest.Foo = "nest"; nest.Parent.Foo = "parent"; nest.Parent.First.Foo = "first"; nest.Parent.Second.Foo = "second"; nest.Single.Foo = "single"; INestParentService n1 = services.Get <INestParentService>(scope); Assert.Equal(nest.Foo, n1.Foo); Assert.Equal(nest.Single.Foo, n1.Single.Foo); Assert.Equal(nest.Parent.Foo, n1.Parent.Foo); Assert.Equal(nest.Parent.First.Foo, n1.Parent.First.Foo); Assert.Equal(nest.Parent.Second.Foo, n1.Parent.Second.Foo); IParentService parent = services.Get <IParentService>(scope); Assert.Equal(nest.Parent.Foo, parent.Foo); Assert.Equal(nest.Parent.First.Foo, parent.First.Foo); Assert.Equal(nest.Parent.Second.Foo, parent.Second.Foo); ISingleService single = services.Get <ISingleService>(scope); Assert.Equal(nest.Single.Foo, single.Foo); IFirstChildService first = services.Get <IFirstChildService>(scope); Assert.Equal(nest.Parent.First.Foo, first.Foo); ISecondChildService second = services.Get <ISecondChildService>(scope); Assert.Equal(nest.Parent.Second.Foo, second.Foo); //scopeless should throw error Assert.Throws <ScopeException>(() => services.Get <INestParentService>()); //another scope should not equal IContainerScope scope2 = services.CreateScope(); INestParentService n3 = services.Get <INestParentService>(scope2); Assert.NotEqual(nest.Foo, n3.Foo); Assert.NotEqual(nest.Single.Foo, n3.Single.Foo); Assert.NotEqual(nest.Parent.Foo, n3.Parent.Foo); Assert.NotEqual(nest.Parent.First.Foo, n3.Parent.First.Foo); Assert.NotEqual(nest.Parent.Second.Foo, n3.Parent.Second.Foo); }
public async Task ScopedInSingleton() { ServiceContainer services = new ServiceContainer(); services.AddSingleton <IParentService, ParentService>(); services.AddScoped <IFirstChildService, FirstChildService>(); services.AddScoped <ISecondChildService, SecondChildService>(); await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <IParentService>()); await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <IFirstChildService>()); await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <ISecondChildService>()); IContainerScope scope = services.CreateScope(); 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.Equal(parent.Foo, p1.Foo); Assert.Equal(parent.First.Foo, p1.First.Foo); Assert.Equal(parent.Second.Foo, p1.Second.Foo); //in same scope, individual scoped items should equal IFirstChildService f1 = await services.Get <IFirstChildService>(scope); ISecondChildService s1 = await services.Get <ISecondChildService>(scope); Assert.Equal(parent.First.Foo, f1.Foo); Assert.Equal(parent.Second.Foo, s1.Foo); //scoped services in singleton should equal (because parent is 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); //but individual created scoped items in different scope should not equal IFirstChildService f2 = await services.Get <IFirstChildService>(scope2); ISecondChildService s2 = await services.Get <ISecondChildService>(scope2); Assert.NotEqual(parent.First.Foo, f2.Foo); Assert.NotEqual(parent.Second.Foo, s2.Foo); }
public async void Nested() { ServiceContainer services = new ServiceContainer(); services.AddScoped <IParentService, ParentService>(); services.AddScoped <IFirstChildService, FirstChildService>(); services.AddScoped <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 p1 = await services.Get <IParentService>(scope); Assert.Equal(parent.Foo, p1.Foo); Assert.Equal(parent.First, p1.First); Assert.Equal(parent.Second, p1.Second); Assert.Equal(parent.First.Foo, p1.First.Foo); Assert.Equal(parent.Second.Foo, p1.Second.Foo); IFirstChildService first = await services.Get <IFirstChildService>(scope); ISecondChildService second = await services.Get <ISecondChildService>(scope); Assert.Equal(parent.First, first); Assert.Equal(parent.First.Foo, first.Foo); Assert.Equal(parent.Second.Foo, second.Foo); //scopeless should throw error await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <IParentService>()); await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <IFirstChildService>()); await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <ISecondChildService>()); //another scope should not equal IContainerScope scope2 = services.CreateScope(); IParentService p3 = await services.Get <IParentService>(scope2); Assert.NotEqual(parent.Foo, p3.Foo); Assert.NotEqual(parent.First, p3.First); Assert.NotEqual(parent.Second, p3.Second); Assert.NotEqual(parent.First.Foo, p3.First.Foo); Assert.NotEqual(parent.Second.Foo, p3.Second.Foo); }
public void Single() { ServiceContainer services = new ServiceContainer(); services.AddScoped <ISingleService, SingleService>(); IContainerScope scope = services.CreateScope(); ISingleService s1 = services.Get <ISingleService>(scope); s1.Foo = "a"; ISingleService s2 = services.Get <ISingleService>(scope); Assert.Equal(s1.Foo, s2.Foo); //scopeless should throw error Assert.Throws <ScopeException>(() => services.Get <ISingleService>()); //another scope should not equal IContainerScope scope2 = services.CreateScope(); ISingleService s4 = services.Get <ISingleService>(scope2); Assert.NotEqual(s1.Foo, s4.Foo); Assert.NotEqual(s2.Foo, s4.Foo); }
public async void Single() { ServiceContainer services = new ServiceContainer(); services.AddScoped <ISingleService, SingleService>(); IContainerScope scope = services.CreateScope(); ISingleService s1 = await services.Get <ISingleService>(scope); s1.Foo = "a"; ISingleService s2 = await services.Get <ISingleService>(scope); Assert.Equal(s1.Foo, s2.Foo); //scopeless should throw error await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <ISingleService>()); //another scope should not equal IContainerScope scope2 = services.CreateScope(); ISingleService s4 = await services.Get <ISingleService>(scope2); Assert.NotEqual(s1.Foo, s4.Foo); Assert.NotEqual(s2.Foo, s4.Foo); }
public void TransientInScoped() { ServiceContainer services = new ServiceContainer(); services.AddScoped <IParentService, ParentService>(); services.AddTransient <IFirstChildService, FirstChildService>(); services.AddTransient <ISecondChildService, SecondChildService>(); IContainerScope scope = services.CreateScope(); IParentService parent = services.Get <IParentService>(scope); parent.Foo = "parent"; parent.First.Foo = "first"; parent.Second.Foo = "second"; //services in scoped service is created only once (cuz they created via parent) IParentService p = 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 first = services.Get <IFirstChildService>(scope); Assert.NotEqual(parent.First.Foo, first.Foo); ISecondChildService second = services.Get <ISecondChildService>(scope); Assert.NotEqual(parent.Second.Foo, second.Foo); }
public async Task PoolsInScoped() { ServiceContainer services = new ServiceContainer(); services.AddScoped <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 scoped) 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); 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 SingletonInScoped() { ServiceContainer services = new ServiceContainer(); services.AddScoped <IParentService, ParentService>(); services.AddSingleton <IFirstChildService, FirstChildService>(); services.AddSingleton <ISecondChildService, SecondChildService>(); IContainerScope scope = services.CreateScope(); IFirstChildService first = services.Get <IFirstChildService>(scope); first.Foo = "first"; IParentService parent = services.Get <IParentService>(scope); parent.Foo = "parent"; parent.Second.Foo = "second"; Assert.Equal(first.Foo, parent.First.Foo); ISecondChildService second = services.Get <ISecondChildService>(scope); Assert.Equal(second.Foo, parent.Second.Foo); IParentService p1 = services.Get <IParentService>(scope); Assert.Equal(p1.Foo, parent.Foo); Assert.Equal(p1.First.Foo, parent.First.Foo); Assert.Equal(p1.Second.Foo, parent.Second.Foo); IContainerScope scope2 = services.CreateScope(); IParentService p2 = services.Get <IParentService>(scope2); Assert.NotEqual(p2.Foo, parent.Foo); Assert.Equal(p2.First.Foo, parent.First.Foo); Assert.Equal(p2.Second.Foo, parent.Second.Foo); }
public static void BuiltInIocTest() { using (IServiceContainer container = new ServiceContainer()) { container.AddSingleton <IConfiguration>(new ConfigurationBuilder() .AddJsonFile("appsettings.json") .Build() ); container.AddScoped <IFly, MonkeyKing>(); container.AddScoped <IFly, Superman>(); container.AddScoped <HasDependencyTest>(); container.AddScoped <HasDependencyTest1>(); container.AddScoped <HasDependencyTest2>(); container.AddScoped <HasDependencyTest3>(); container.AddScoped(typeof(HasDependencyTest4 <>)); container.AddTransient <WuKong>(); container.AddScoped <WuJing>(serviceProvider => new WuJing()); container.AddSingleton(typeof(GenericServiceTest <>)); var rootConfig = container.ResolveService <IConfiguration>(); //try //{ // container.ResolveService<IFly>(); //} //catch (Exception e) //{ // Console.WriteLine(e); //} using (var scope = container.CreateScope()) { var config = scope.ResolveService <IConfiguration>(); var fly1 = scope.ResolveService <IFly>(); var fly2 = scope.ResolveService <IFly>(); var wukong1 = scope.ResolveService <WuKong>(); var wukong2 = scope.ResolveService <WuKong>(); var wuJing1 = scope.ResolveService <WuJing>(); var wuJing2 = scope.ResolveService <WuJing>(); Console.WriteLine("fly1 == fly2, {0}", fly1 == fly2); Console.WriteLine("rootConfig == config, {0}", rootConfig == config); Console.WriteLine("wukong1 == wukong2, {0}", wukong1 == wukong2); Console.WriteLine("wujing1 == wujing2, {0}", wuJing1 == wuJing2); fly1.Fly(); wukong1.Jump(); wukong2.Jump(); wuJing1.Eat(); var s0 = scope.ResolveRequiredService <HasDependencyTest>(); s0.Test(); Console.WriteLine($"s0._fly == fly1 : {s0._fly == fly1}"); var s1 = scope.ResolveService <HasDependencyTest1>(); s1.Test(); var s2 = scope.ResolveService <HasDependencyTest2>(); s2.Test(); var s3 = scope.ResolveService <HasDependencyTest3>(); s3.Test(); var s4 = scope.ResolveService <HasDependencyTest4 <string> >(); s4.Test(); using (var innerScope = scope.CreateScope()) { var config2 = innerScope.ResolveRequiredService <IConfiguration>(); Console.WriteLine("rootConfig == config2, {0}", rootConfig == config2); var fly3 = innerScope.ResolveRequiredService <IFly>(); fly3.Fly(); } var number = config.GetAppSetting <int>("Number"); Console.WriteLine(number); var flySvcs = scope.ResolveServices <IFly>(); foreach (var f in flySvcs) { f.Fly(); } } var genericService1 = container.ResolveRequiredService <GenericServiceTest <int> >(); genericService1.Test(); var genericService2 = container.ResolveRequiredService <GenericServiceTest <string> >(); genericService2.Test(); } }