Ejemplo n.º 1
0
        public void TransientInTransientPool()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransientPool <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";

            IParentService p = services.Get <IParentService>(scope);

            Assert.NotEqual(parent.Foo, p.Foo);
            Assert.NotEqual(parent.First.Foo, p.First.Foo);
            Assert.NotEqual(parent.Second.Foo, p.Second.Foo);

            IFirstChildService f1 = services.Get <IFirstChildService>();
            IFirstChildService f2 = services.Get <IFirstChildService>(scope);

            Assert.NotEqual(parent.First.Foo, f1.Foo);
            Assert.NotEqual(parent.First.Foo, f2.Foo);

            ISecondChildService s1 = services.Get <ISecondChildService>();
            ISecondChildService s2 = services.Get <ISecondChildService>(scope);

            Assert.NotEqual(parent.Second.Foo, s1.Foo);
            Assert.NotEqual(parent.Second.Foo, s2.Foo);
        }
Ejemplo n.º 2
0
        public async Task TransientInScoped()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddScoped <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";

            //services in scoped service is created only once (cuz they created via parent)
            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 first = await services.Get <IFirstChildService>(scope);

            Assert.NotEqual(parent.First.Foo, first.Foo);

            ISecondChildService second = await services.Get <ISecondChildService>(scope);

            Assert.NotEqual(parent.Second.Foo, second.Foo);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        public void TransientInSingleton()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddSingleton <IParentService, ParentService>();
            services.AddTransient <IFirstChildService, FirstChildService>();
            services.AddTransient <ISecondChildService, SecondChildService>();

            IParentService parent = services.Get <IParentService>();

            parent.Foo        = "parent";
            parent.First.Foo  = "first";
            parent.Second.Foo = "second";

            //services in singleton service is created only once (cuz they created via parent)
            IParentService p = services.Get <IParentService>();

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

            Assert.NotEqual(parent.First.Foo, first.Foo);

            ISecondChildService second = services.Get <ISecondChildService>();

            Assert.NotEqual(parent.Second.Foo, second.Foo);
        }
Ejemplo n.º 5
0
        public void SingletonInTransient()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransient <IParentService, ParentService>();
            services.AddSingleton <IFirstChildService, FirstChildService>();
            services.AddSingleton <ISecondChildService, SecondChildService>();

            IFirstChildService first = services.Get <IFirstChildService>();

            first.Foo = "first";

            IParentService parent = services.Get <IParentService>();

            parent.Foo        = "parent";
            parent.Second.Foo = "second";
            Assert.Equal(first.Foo, parent.First.Foo);

            ISecondChildService second = services.Get <ISecondChildService>();

            Assert.Equal(second.Foo, parent.Second.Foo);

            IParentService p = services.Get <IParentService>();

            Assert.NotEqual(p.Foo, parent.Foo);
            Assert.Equal(p.First.Foo, parent.First.Foo);
            Assert.Equal(p.Second.Foo, parent.Second.Foo);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        public async Task MultipleNestedDoubleParameter()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddSingleton <INestParentService, NestParentService>();
            services.AddSingleton <ISingleService, SingleService>();
            services.AddSingleton <IParentService, ParentService>();
            services.AddSingleton <IFirstChildService, FirstChildService>();
            services.AddSingleton <ISecondChildService, SecondChildService>();

            INestParentService nest = await services.Get <INestParentService>();

            nest.Foo               = "nest";
            nest.Parent.Foo        = "parent";
            nest.Parent.First.Foo  = "first";
            nest.Parent.Second.Foo = "second";
            nest.Single.Foo        = "single";

            INestParentService n1 = await services.Get <INestParentService>();

            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 = await services.Get <IParentService>();

            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 = await services.Get <ISingleService>();

            Assert.Equal(nest.Single.Foo, single.Foo);

            IFirstChildService first = await services.Get <IFirstChildService>();

            Assert.Equal(nest.Parent.First.Foo, first.Foo);

            ISecondChildService second = await services.Get <ISecondChildService>();

            Assert.Equal(nest.Parent.Second.Foo, second.Foo);

            IContainerScope    scope = services.CreateScope();
            INestParentService n2    = await services.Get <INestParentService>(scope);

            Assert.Equal(nest.Foo, n2.Foo);
            Assert.Equal(nest.Single.Foo, n2.Single.Foo);
            Assert.Equal(nest.Parent.Foo, n2.Parent.Foo);
            Assert.Equal(nest.Parent.First.Foo, n2.Parent.First.Foo);
            Assert.Equal(nest.Parent.Second.Foo, n2.Parent.Second.Foo);
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        public async void Nested()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransient <IParentService, ParentService>();
            services.AddTransient <IFirstChildService, FirstChildService>();
            services.AddTransient <ISecondChildService, SecondChildService>();

            IParentService parent = await services.Get <IParentService>();

            parent.Foo        = "parent";
            parent.First.Foo  = "first";
            parent.Second.Foo = "second";

            IParentService p1 = await services.Get <IParentService>();

            Assert.NotEqual(parent.Foo, p1.Foo);
            Assert.NotEqual(parent.First.Foo, p1.First.Foo);
            Assert.NotEqual(parent.Second.Foo, p1.Second.Foo);

            IFirstChildService first = await services.Get <IFirstChildService>();

            ISecondChildService second = await services.Get <ISecondChildService>();

            Assert.NotEqual(parent.First.Foo, first.Foo);
            Assert.NotEqual(parent.Second.Foo, second.Foo);

            //scoped
            IContainerScope scope = services.CreateScope();
            IParentService  p2    = await services.Get <IParentService>(scope);

            Assert.NotEqual(parent.Foo, p2.Foo);
            Assert.NotEqual(parent.First, p2.First);
            Assert.NotEqual(parent.Second, p2.Second);
            Assert.NotEqual(parent.First.Foo, p2.First.Foo);
            Assert.NotEqual(parent.Second.Foo, p2.Second.Foo);

            IFirstChildService firstScoped = await services.Get <IFirstChildService>(scope);

            ISecondChildService secondScoped = await services.Get <ISecondChildService>(scope);

            Assert.NotEqual(parent.First, firstScoped);
            Assert.NotEqual(parent.First.Foo, firstScoped.Foo);
            Assert.NotEqual(parent.Second.Foo, secondScoped.Foo);
        }
Ejemplo n.º 12
0
        public void PoolsInScopedPool()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddScopedPool <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);
        }
Ejemplo n.º 13
0
        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);
        }
Ejemplo n.º 14
0
        public void PoolsInTransient()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransient <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);
        }
Ejemplo n.º 15
0
        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);
        }
Ejemplo n.º 16
0
        public async Task Nested()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddSingleton <IParentService, ParentService>();
            services.AddSingleton <IFirstChildService, FirstChildService>();
            services.AddSingleton <ISecondChildService, SecondChildService>();

            IParentService singleton = await services.Get <IParentService>();

            singleton.Foo        = "singleton";
            singleton.First.Foo  = "first";
            singleton.Second.Foo = "second";

            IParentService p1 = await services.Get <IParentService>();

            Assert.Equal(singleton.Foo, p1.Foo);
            Assert.Equal(singleton.First, p1.First);
            Assert.Equal(singleton.Second, p1.Second);
            Assert.Equal(singleton.First.Foo, p1.First.Foo);
            Assert.Equal(singleton.Second.Foo, p1.Second.Foo);

            IFirstChildService first = await services.Get <IFirstChildService>();

            Assert.Equal(singleton.First.Foo, first.Foo);

            ISecondChildService second = await services.Get <ISecondChildService>();

            Assert.Equal(singleton.Second.Foo, second.Foo);

            IContainerScope scope = services.CreateScope();
            IParentService  p2    = await services.Get <IParentService>(scope);

            Assert.Equal(singleton.Foo, p2.Foo);
            Assert.Equal(singleton.First, p2.First);
            Assert.Equal(singleton.Second, p2.Second);
            Assert.Equal(singleton.First.Foo, p2.First.Foo);
            Assert.Equal(singleton.Second.Foo, p2.Second.Foo);
        }
Ejemplo n.º 17
0
        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);
        }
Ejemplo n.º 18
0
 public ParentService(IFirstChildService firstChildService, ISecondChildService secondChildService)
 {
     First  = firstChildService;
     Second = secondChildService;
 }
Ejemplo n.º 19
0
        public void MultipleNestedDoubleParameter()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransient <INestParentService, NestParentService>();
            services.AddTransient <ISingleService, SingleService>();
            services.AddTransient <IParentService, ParentService>();
            services.AddTransient <IFirstChildService, FirstChildService>();
            services.AddTransient <ISecondChildService, SecondChildService>();

            INestParentService nest = services.Get <INestParentService>();

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

            Assert.NotEqual(nest.Foo, n1.Foo);
            Assert.NotEqual(nest.Single.Foo, n1.Single.Foo);
            Assert.NotEqual(nest.Parent.Foo, n1.Parent.Foo);
            Assert.NotEqual(nest.Parent.First.Foo, n1.Parent.First.Foo);
            Assert.NotEqual(nest.Parent.Second.Foo, n1.Parent.Second.Foo);

            IParentService parent = services.Get <IParentService>();

            Assert.NotEqual(nest.Parent.Foo, parent.Foo);
            Assert.NotEqual(nest.Parent.First.Foo, parent.First.Foo);
            Assert.NotEqual(nest.Parent.Second.Foo, parent.Second.Foo);

            ISingleService single = services.Get <ISingleService>();

            Assert.NotEqual(nest.Single.Foo, single.Foo);

            IFirstChildService first = services.Get <IFirstChildService>();

            Assert.NotEqual(nest.Parent.First.Foo, first.Foo);

            ISecondChildService second = services.Get <ISecondChildService>();

            Assert.NotEqual(nest.Parent.Second.Foo, second.Foo);

            IContainerScope scope = services.CreateScope();

            INestParentService n2 = services.Get <INestParentService>(scope);

            Assert.NotEqual(nest.Foo, n2.Foo);
            Assert.NotEqual(nest.Single.Foo, n2.Single.Foo);
            Assert.NotEqual(nest.Parent.Foo, n2.Parent.Foo);
            Assert.NotEqual(nest.Parent.First.Foo, n2.Parent.First.Foo);
            Assert.NotEqual(nest.Parent.Second.Foo, n2.Parent.Second.Foo);
            n2.Foo               = "nest-2";
            n2.Parent.Foo        = "parent-2";
            n2.Parent.First.Foo  = "first-2";
            n2.Parent.Second.Foo = "second-2";
            n2.Single.Foo        = "single-2";

            INestParentService n3 = services.Get <INestParentService>(scope);

            Assert.NotEqual(n2.Foo, n3.Foo);
            Assert.NotEqual(n2.Single.Foo, n3.Single.Foo);
            Assert.NotEqual(n2.Parent.Foo, n3.Parent.Foo);
            Assert.NotEqual(n2.Parent.First.Foo, n3.Parent.First.Foo);
            Assert.NotEqual(n2.Parent.Second.Foo, n3.Parent.Second.Foo);
        }