Exemplo n.º 1
0
        public void AddSingleton()
        {
            var ioc      = new ObjectContainer();
            var services = ioc.GetValue("_list") as IList <IObject>;

            ioc.AddSingleton(typeof(ICache), typeof(Redis));
            Assert.Equal(1, ioc.Count);
            Assert.Equal(1, services.Count);
            Assert.True(ioc.Resolve <ICache>() is Redis);

            ioc.AddSingleton <ICache, MemoryCache>();
            Assert.Equal(1, ioc.Count);
            Assert.True(ioc.Resolve <ICache>() is MemoryCache);

            ioc.AddSingleton(typeof(ICache), p => new Redis());
            Assert.True(ioc.Resolve <ICache>() is Redis);

            ioc.AddSingleton <ICache>(p => new MemoryCache());
            Assert.True(ioc.Resolve <ICache>() is MemoryCache);

            ioc.AddSingleton(typeof(ICache), new Redis());
            Assert.True(ioc.Resolve <ICache>() is Redis);

            ioc.TryAddSingleton(typeof(ICache), typeof(MemoryCache));
            Assert.True(ioc.Resolve <ICache>() is Redis);

            Assert.Equal(1, services.Count);
            Assert.Equal(ObjectLifetime.Singleton, services[0].Lifttime);
        }
Exemplo n.º 2
0
        public void AddRedis()
        {
            var ioc = new ObjectContainer();

            var config = new ConfigProvider();

            config["orderRedis"] = "server=127.0.0.1:6379;password=pass;db=7";
            ioc.AddSingleton <IConfigProvider>(config);
            ioc.AddSingleton(provider => new Redis(provider, "orderRedis"));

            var prv = ioc.BuildServiceProvider();

            var rds = prv.GetService <Redis>();

            Assert.Equal("127.0.0.1:6379", rds.Server);
            Assert.Equal("pass", rds.Password);
            Assert.Equal(7, rds.Db);

            // 改变配置数据,影响对象属性
            config["orderRedis"] = "server=10.0.0.1:6379;password=word;db=13";
            config.SaveAll();

            Assert.Equal("10.0.0.1:6379", rds.Server);
            Assert.Equal("word", rds.Password);
            Assert.Equal(13, rds.Db);
        }
Exemplo n.º 3
0
        public void TestMutilConstructor()
        {
            {
                var ioc = new ObjectContainer();
                ioc.AddSingleton <ICache, Redis>();
                ioc.AddTransient <MyService>();

                var svc = ioc.Resolve <MyService>();
                Assert.Equal(1, svc.Kind);
            }

            {
                var ioc = new ObjectContainer();
                ioc.AddSingleton <Redis>();
                ioc.AddTransient <MyService>();

                var svc = ioc.Resolve <MyService>();
                Assert.Equal(2, svc.Kind);
            }

            {
                var ioc = new ObjectContainer();
                ioc.AddSingleton <ICache, Redis>();
                ioc.AddTransient <MyService>();

                var svc = ioc.Resolve <MyService>();
                Assert.Equal(1, svc.Kind);
            }

            {
                var ioc = new ObjectContainer();
                ioc.AddSingleton <ICache, Redis>();
                ioc.AddTransient <MyService>();

                var svc = ioc.Resolve <MyService>();
                Assert.Equal(1, svc.Kind);
            }

            {
                var ioc = new ObjectContainer();
                ioc.AddSingleton <ICache, Redis>();
                ioc.AddSingleton <ILog>(XTrace.Log);
                ioc.AddTransient <MyService>();

                var svc = ioc.Resolve <MyService>();
                Assert.Equal(3, svc.Kind);
            }
        }