Esempio n. 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);
            }
        }
        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);
        }
        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);
        }
Esempio n. 5
0
        public void RegisterByAttribute()
        {
            var log = new InMemoryLog();

            using (var container = new TactContainer(log))
            {
                Assert.Throws <InvalidOperationException>(() => container.Resolve <ITester>());

                container.RegisterByAttribute(typeof(Tester));
                var tester = container.Resolve <ITester>();
                Assert.IsType <Tester>(tester);
            }
        }
        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);
            }
        }
Esempio n. 7
0
        public void GenericClassResolve()
        {
            using (var resolver = new TactContainer(new InMemoryLog()))
            {
                Assert.Throws <InvalidOperationException>(() => resolver.Resolve <Seven>());

                var eightInt = resolver.Resolve <Eight <int> >();
                Assert.Equal(typeof(int), eightInt.Type);

                var eightBool = resolver.Resolve <Eight <bool> >();
                Assert.Equal(typeof(bool), eightBool.Type);
            }
        }
Esempio n. 8
0
        public void GenericInterfaceResolve()
        {
            using (var resolver = new TactContainer(new InMemoryLog()))
            {
                resolver.RegisterTransient(typeof(IEight <>), typeof(Eight <>));

                Assert.Throws <InvalidOperationException>(() => resolver.Resolve <Seven>());

                var eightInt = resolver.Resolve <IEight <int> >();
                Assert.Equal(typeof(int), eightInt.Type);

                var eightBool = resolver.Resolve <IEight <bool> >();
                Assert.Equal(typeof(bool), eightBool.Type);
            }
        }
Esempio n. 9
0
 public void PreventRecursion()
 {
     using (var resolver = new TactContainer(new InMemoryLog()))
     {
         resolver.RegisterTransient <Seven>();
         Assert.Throws <InvalidOperationException>(() => resolver.Resolve <Seven>());
     }
 }
Esempio n. 10
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());
            }
        }
        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);
                }
            }
        }
Esempio n. 12
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);
            }
        }
Esempio n. 13
0
        public void ShouldRegisterFalse()
        {
            var logger = new InMemoryLog();

            using (var container = new TactContainer(logger))
            {
                var map = new Dictionary <string, string>
                {
                    { "TestConfig.ShouldRegister", "false" }
                };
                var configBuilder = new ConfigurationBuilder();
                configBuilder.AddInMemoryCollection(map);
                var config = configBuilder.Build();

                var types = new[] { typeof(TestConfig), typeof(Test) };
                container.ConfigureByAttribute(config, types);
                container.RegisterByAttribute(types);

                Assert.Throws <InvalidOperationException>(() => container.Resolve <ITest>());
            }
        }
Esempio n. 14
0
        public void ShouldRegisterTrue()
        {
            var logger = new InMemoryLog();

            using (var container = new TactContainer(logger))
            {
                var map = new Dictionary <string, string>
                {
                    { "TestConfig:ShouldRegister", "true" }
                };
                var configBuilder = new ConfigurationBuilder();
                configBuilder.AddInMemoryCollection(map);
                var config = configBuilder.Build();

                var types = new[] { typeof(TestConfig), typeof(Test) };
                container.ConfigureByAttribute(config, types);
                container.RegisterByAttribute(types);

                container.Resolve <ITest>();
            }
        }
Esempio n. 15
0
 public void ThrowOnFail()
 {
     using (var resolver = new TactContainer(new InMemoryLog()))
         Assert.Throws <InvalidOperationException>(() => resolver.Resolve <IOne>());
 }