public void RegisterPerScope()
        {
            IOne a, b, c, d, e, f, g, h;

            using (var resolver = new TactContainer(new InMemoryLog()))
            {
                resolver.RegisterPerScope <IOne, One>();

                a = resolver.Resolve <IOne>();
                b = resolver.Resolve <IOne>();

                Assert.Same(a, b);
                Assert.False(a.IsDisposed);

                using (var child1 = resolver.BeginScope())
                {
                    c = child1.Resolve <IOne>();
                    d = child1.Resolve <IOne>();

                    Assert.NotSame(c, a);
                    Assert.Same(c, d);
                    Assert.False(c.IsDisposed);

                    using (var child2 = resolver.BeginScope())
                    {
                        e = child2.Resolve <IOne>();
                        f = child2.Resolve <IOne>();

                        Assert.NotSame(e, a);
                        Assert.NotSame(e, c);
                        Assert.Same(e, f);
                        Assert.False(e.IsDisposed);
                    }

                    using (var child3 = resolver.BeginScope())
                    {
                        g = child3.Resolve <IOne>();
                        h = child3.Resolve <IOne>();

                        Assert.NotSame(g, a);
                        Assert.NotSame(g, c);
                        Assert.NotSame(g, d);
                        Assert.Same(g, h);
                        Assert.False(g.IsDisposed);
                    }

                    Assert.False(a.IsDisposed);
                    Assert.False(c.IsDisposed);
                    Assert.True(e.IsDisposed);
                    Assert.True(g.IsDisposed);
                }
            }

            Assert.True(a.IsDisposed);
            Assert.True(c.IsDisposed);
        }
        public void RegisterTransient()
        {
            IOne a, b, c, d;

            using (var resolver = new TactContainer(new InMemoryLog()))
            {
                resolver.RegisterTransient <IOne, One>();

                a = resolver.Resolve <IOne>();
                b = resolver.Resolve <IOne>();

                using (var child = resolver.BeginScope())
                {
                    c = child.Resolve <IOne>();
                    d = child.Resolve <IOne>();
                }
            }

            Assert.NotSame(a, b);
            Assert.NotSame(a, c);
            Assert.NotSame(a, d);

            Assert.False(a.IsDisposed);
            Assert.False(b.IsDisposed);
            Assert.False(c.IsDisposed);
            Assert.False(d.IsDisposed);
        }
        public void RegisterSingleton()
        {
            IOne a, b, c, d;

            using (var resolver = new TactContainer(new InMemoryLog()))
            {
                resolver.RegisterSingleton <IOne, One>();

                a = resolver.Resolve <IOne>();
                b = resolver.Resolve <IOne>();

                using (var child = resolver.BeginScope())
                {
                    c = child.Resolve <IOne>();
                    d = child.Resolve <IOne>();
                }

                Assert.Same(a, b);
                Assert.Same(a, c);
                Assert.Same(a, d);

                Assert.False(a.IsDisposed);
            }

            Assert.True(a.IsDisposed);
        }
Exemplo n.º 4
0
        public static void Container()
        {
            Thread.Sleep(2000);
            System.Console.WriteLine("Start");

            using (var container = new TactContainer(new EmptyLog()))
            {
                container.RegisterPerScope <IOne, One>();
                container.RegisterPerScope <ITwo, Two>();

                container.RegisterSingleton <IThree, Three>();
                container.RegisterSingleton <IFour, Four>();
                container.RegisterSingleton <IFive, Five>();
                container.RegisterSingleton <ISix, Six>();
                container.RegisterSingleton <ISeven, Seven>();
                container.RegisterSingleton <IEigth, Eight>();
                container.RegisterSingleton <INine, Nine>();
                container.RegisterSingleton <ITen, Ten>();

                var sw1 = Stopwatch.StartNew();
                using (var scope = container.BeginScope())
                {
                    scope.Resolve <IOne>();
                    scope.Resolve <ITen>();
                }
                sw1.Stop();

                var sw2 = Stopwatch.StartNew();
                for (var i = 0; i < 1000000; i++)
                {
                    using (var scope = container.BeginScope())
                    {
                        scope.Resolve <IOne>();
                        scope.Resolve <ITen>();
                    }
                }
                sw2.Stop();

                System.Console.WriteLine(sw1.ElapsedMilliseconds.ToString());
                System.Console.WriteLine(sw2.ElapsedMilliseconds.ToString());
            }

            System.Console.WriteLine("Stop");
            Thread.Sleep(2000);
        }
        public void PerScopeProxy()
        {
            using (var resolver = new TactContainer(new InMemoryLog()))
            {
                resolver.RegisterPerScope <IOne, One>();
                resolver.RegisterProxy <ITwo, IOne>();

                var twoA = resolver.Resolve <ITwo>();
                var oneA = resolver.Resolve <IOne>();

                Assert.Same(oneA, twoA);

                using (var scope = resolver.BeginScope())
                {
                    var twoB = scope.Resolve <ITwo>();
                    var oneB = scope.Resolve <IOne>();

                    Assert.Same(oneB, twoB);
                    Assert.NotSame(oneA, oneB);
                }
            }
        }