コード例 #1
0
        public void LazyResolve()
        {
            using (var resolver = new TactContainer(new InMemoryLog()))
            {
                resolver.RegisterSingleton <IOne, One>();

                var a = resolver.Resolve <Three>();
                var b = resolver.Resolve <Three>();

                Assert.False(a.LazyOne.IsValueCreated);
                Assert.False(b.LazyOne.IsValueCreated);

                Assert.IsType <One>(a.LazyOne.Value);

                Assert.True(a.LazyOne.IsValueCreated);
                Assert.False(b.LazyOne.IsValueCreated);

                Assert.IsType <One>(b.LazyOne.Value);

                Assert.True(a.LazyOne.IsValueCreated);
                Assert.True(b.LazyOne.IsValueCreated);

                Assert.Same(a.LazyOne.Value, b.LazyOne.Value);
            }
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        public void EnumerableResolve()
        {
            using (var resolver = new TactContainer(new InMemoryLog()))
            {
                resolver.RegisterSingleton <ITwo, One>("Two");

                var a = resolver.Resolve <Four>();
                Assert.IsType <One>(a.Twos.Single());
            }
        }
コード例 #4
0
        public void DoNotIncludeUnkeyedInResolveAll()
        {
            using (var resolver = new TactContainer(new InMemoryLog()))
            {
                resolver.RegisterSingleton <IOne, One>();

                var a = resolver.ResolveAll <IOne>().ToList();
                Assert.Equal(0, a.Count);

                resolver.RegisterSingleton <IOne, One>("Doh");

                var b = resolver.ResolveAll <IOne>().ToList();
                Assert.Equal(1, b.Count);

                resolver.RegisterSingleton <IOne, One>("Ray");

                var c = resolver.ResolveAll <IOne>().ToList();
                Assert.Equal(2, c.Count);
            }
        }
コード例 #5
0
        public void SingletonProxy()
        {
            using (var resolver = new TactContainer(new InMemoryLog()))
            {
                resolver.RegisterSingleton <IOne, One>();
                resolver.RegisterProxy <ITwo, IOne>();

                var two = resolver.Resolve <ITwo>();
                var one = resolver.Resolve <IOne>();

                Assert.Same(one, two);
            }
        }
コード例 #6
0
        public void FuncResolve()
        {
            using (var resolver = new TactContainer(new InMemoryLog()))
            {
                resolver.RegisterSingleton <ITwo, One>();

                var a = resolver.Resolve <Five>();
                var b = a.Func();
                var c = a.Func();

                Assert.IsType <One>(b);
                Assert.Same(b, c);
            }
        }
コード例 #7
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);
        }