Example #1
0
        public void Setup()
        {
            var services = new ServiceCollection();

            services.AddSingleton <A>();
            services.AddSingleton <B>();
            services.AddSingleton <C>();
            _singletonSp = services.BuildServiceProvider();

            services = new ServiceCollection();
            services.AddScoped <A>();
            services.AddScoped <B>();
            services.AddScoped <C>();
            _scopedSp = services.BuildServiceProvider().CreateScope();

            services = new ServiceCollection();
            services.AddTransient <A>();
            services.AddTransient <B>();
            services.AddTransient <C>();
            _transientSp = services.BuildServiceProvider();

            var containerBuilder = new ServiceContainerBuilder();

            containerBuilder.AddSingleton <A>();
            containerBuilder.AddSingleton <B>();
            containerBuilder.AddSingleton <C>();

            _singletonContainer = containerBuilder.Build();

            containerBuilder = new ServiceContainerBuilder();
            containerBuilder.AddScoped <A>();
            containerBuilder.AddScoped <B>();
            containerBuilder.AddScoped <C>();

            _scopedRootContainer = containerBuilder.Build();
            _scopedContainer     = _scopedRootContainer.CreateScope();

            containerBuilder = new ServiceContainerBuilder();
            containerBuilder.AddTransient <A>();
            containerBuilder.AddTransient <B>();
            containerBuilder.AddTransient <C>();

            _transientContainer = containerBuilder.Build();
        }
Example #2
0
        public DependencyInjectionTest()
        {
            var containerBuilder = new ServiceContainerBuilder();

            containerBuilder.AddSingleton <IConfiguration>(new ConfigurationBuilder().Build());
            containerBuilder.AddScoped <IFly, MonkeyKing>();
            containerBuilder.AddScoped <IFly, Superman>();

            containerBuilder.AddScoped <HasDependencyTest>();
            containerBuilder.AddScoped <HasDependencyTest1>();
            containerBuilder.AddScoped <HasDependencyTest2>();
            containerBuilder.AddScoped <HasDependencyTest3>();
            containerBuilder.AddScoped(typeof(HasDependencyTest4 <>));

            containerBuilder.AddTransient <WuKong>();
            containerBuilder.AddScoped <WuJing>(serviceProvider => new WuJing());
            containerBuilder.AddSingleton(typeof(GenericServiceTest <>));

            _container = containerBuilder.Build();
        }
        public static void BuiltInIocTest()
        {
            IServiceContainerBuilder containerBuilder = new ServiceContainerBuilder();

            containerBuilder.AddSingleton <IConfiguration>(new ConfigurationBuilder()
                                                           .AddJsonFile("appsettings.json")
                                                           .Build()
                                                           );
            containerBuilder.AddScoped <IFly, MonkeyKing>();
            containerBuilder.AddScoped <IFly, Superman>();

            containerBuilder.AddScoped <HasDependencyTest>();
            containerBuilder.AddScoped <HasDependencyTest1>();
            containerBuilder.AddScoped <HasDependencyTest2>();
            containerBuilder.AddScoped <HasDependencyTest3>();
            containerBuilder.AddScoped(typeof(HasDependencyTest4 <>));

            containerBuilder.AddTransient <WuKong>();
            containerBuilder.AddScoped <WuJing>(serviceProvider => new WuJing());
            containerBuilder.AddSingleton(typeof(GenericServiceTest <>));

            containerBuilder.RegisterAssemblyModules();

            using (var container = containerBuilder.Build())
            {
                var idGenerator = container.ResolveRequiredService <IIdGenerator>();
                Console.WriteLine(idGenerator.NewId());
                var rootConfig = container.ResolveService <IConfiguration>();
                //try
                //{
                //    container.ResolveService<IFly>();
                //}
                //catch (Exception e)
                //{
                //    Console.WriteLine(e);
                //}

                using (var scope = container.CreateScope())
                {
                    var config = scope.ResolveService <IConfiguration>();
                    var fly1   = scope.ResolveService <IFly>();
                    var fly2   = scope.ResolveService <IFly>();

                    var wukong1 = scope.ResolveService <WuKong>();
                    var wukong2 = scope.ResolveService <WuKong>();

                    var wuJing1 = scope.ResolveService <WuJing>();
                    var wuJing2 = scope.ResolveService <WuJing>();

                    Console.WriteLine("fly1 == fly2,  {0}", fly1 == fly2);
                    Console.WriteLine("rootConfig == config, {0}", rootConfig == config);
                    Console.WriteLine("wukong1 == wukong2, {0}", wukong1 == wukong2);
                    Console.WriteLine("wujing1 == wujing2, {0}", wuJing1 == wuJing2);

                    fly1.Fly();

                    wukong1.Jump();
                    wukong2.Jump();

                    wuJing1.Eat();

                    var s0 = scope.ResolveRequiredService <HasDependencyTest>();
                    s0.Test();
                    Console.WriteLine($"s0._fly == fly1 : {s0._fly == fly1}");

                    var s1 = scope.ResolveService <HasDependencyTest1>();
                    s1.Test();

                    var s2 = scope.ResolveService <HasDependencyTest2>();
                    s2.Test();

                    var s3 = scope.ResolveService <HasDependencyTest3>();
                    s3.Test();

                    var s4 = scope.ResolveService <HasDependencyTest4 <string> >();
                    s4.Test();

                    using (var innerScope = scope.CreateScope())
                    {
                        var config2 = innerScope.ResolveRequiredService <IConfiguration>();
                        Console.WriteLine("rootConfig == config2, {0}", rootConfig == config2);
                        var fly3 = innerScope.ResolveRequiredService <IFly>();
                        fly3.Fly();
                    }

                    var number = config.GetAppSetting <int>("Number");
                    Console.WriteLine(number);

                    var flySvcs = scope.ResolveServices <IFly>();
                    foreach (var f in flySvcs)
                    {
                        f.Fly();
                    }
                }

                var genericService1 = container.ResolveRequiredService <GenericServiceTest <int> >();
                genericService1.Test();

                var genericService2 = container.ResolveRequiredService <GenericServiceTest <string> >();
                genericService2.Test();
            }
        }