Exemplo n.º 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();
        }
Exemplo n.º 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();
            }
        }
        public static IServiceContainer BuildFluentAspectsContainer(this IServiceContainerBuilder serviceCollection,
                                                                    Action <FluentAspectOptions> optionsAction,
                                                                    Action <IFluentAspectsServiceContainerBuilder> aspectBuildAction = null,
                                                                    Expression <Func <Type, bool> > ignoreTypesFilter = null)
        {
            var services = new ServiceContainerBuilder();

            var aspectBuilder = null != optionsAction
                ? serviceCollection.AddFluentAspects(optionsAction)
                : serviceCollection.AddFluentAspects();

            aspectBuildAction?.Invoke(aspectBuilder);

            Expression <Func <Type, bool> > ignoreTypesExpression = t => "WeihanLi.Common.Aspect".Equals(t.Namespace);

            if (null != ignoreTypesFilter)
            {
                ignoreTypesExpression = ignoreTypesExpression.Or(ignoreTypesFilter);
            }

            var ignoreTypesPredicate = ignoreTypesExpression.Compile();

            using (var serviceProvider = serviceCollection.Build())
            {
                var proxyTypeFactory = serviceProvider.ResolveRequiredService <IProxyTypeFactory>();

                foreach (var descriptor in serviceCollection)
                {
                    if (descriptor.ServiceType.IsSealed ||
                        descriptor.ServiceType.IsNotPublic ||
                        descriptor.ServiceType.IsGenericTypeDefinition
                        )
                    {
                        services.Add(descriptor);
                        continue;
                    }

                    if (ignoreTypesPredicate(descriptor.ServiceType))
                    {
                        services.Add(descriptor);
                        continue;
                    }

                    if (descriptor.ImplementType != null)
                    {
                        if (descriptor.ImplementType.IsNotPublic ||
                            descriptor.ImplementType.IsProxyType()
                            )
                        {
                            services.Add(descriptor);
                            continue;
                        }

                        if (descriptor.ServiceType.IsClass &&
                            descriptor.ImplementType.IsSealed)
                        {
                            services.Add(descriptor);
                            continue;
                        }

                        if (descriptor.ServiceType.IsGenericTypeDefinition ||
                            descriptor.ImplementType.IsGenericTypeDefinition)
                        {
                            var proxyType = proxyTypeFactory.CreateProxyType(descriptor.ServiceType, descriptor.ImplementType);
                            services.Add(new ServiceDefinition(descriptor.ServiceType, proxyType,
                                                               descriptor.ServiceLifetime));
                            continue;
                        }
                    }

                    Func <IServiceProvider, object> serviceFactory = null;

                    if (descriptor.ImplementationInstance != null)
                    {
                        if (descriptor.ImplementationInstance.GetType().IsPublic)
                        {
                            serviceFactory = provider => provider.ResolveRequiredService <IProxyFactory>()
                                             .CreateProxyWithTarget(descriptor.ServiceType, descriptor.ImplementationInstance);
                        }
                    }
                    else if (descriptor.ImplementType != null)
                    {
                        serviceFactory = provider =>
                        {
                            var proxy = provider.ResolveRequiredService <IProxyFactory>()
                                        .CreateProxy(descriptor.ServiceType, descriptor.ImplementType);
                            return(proxy);
                        };
                    }
                    else if (descriptor.ImplementationFactory != null)
                    {
                        serviceFactory = provider =>
                        {
                            var implement = descriptor.ImplementationFactory(provider);
                            if (implement == null)
                            {
                                return(null);
                            }

                            var implementType = implement.GetType();
                            if (implementType.IsNotPublic ||
                                implementType.IsProxyType())
                            {
                                return(implement);
                            }

                            return(provider.ResolveRequiredService <IProxyFactory>()
                                   .CreateProxyWithTarget(descriptor.ServiceType, implement));
                        };
                    }

                    if (null != serviceFactory)
                    {
                        services.Add(new ServiceDefinition(descriptor.ServiceType, serviceFactory,
                                                           descriptor.ServiceLifetime));
                    }
                    else
                    {
                        services.Add(descriptor);
                    }
                }
            }

            var container = services.Build();

            DependencyResolver.SetDependencyResolver(container);
            return(container);
        }
Exemplo n.º 5
0
        public static IServiceContainer BuildFluentAspectsContainer(this IServiceContainerBuilder serviceCollection,
                                                                    Action <FluentAspectOptions> optionsAction,
                                                                    Action <IFluentAspectsServiceContainerBuilder> aspectBuildAction = null,
                                                                    Func <Type, bool> ignoreTypesPredict = null)
        {
            var services = new ServiceContainerBuilder();

            var aspectBuilder = null != optionsAction
                ? services.AddFluentAspects(optionsAction)
                : services.AddFluentAspects();

            aspectBuildAction?.Invoke(aspectBuilder);

            foreach (var descriptor in serviceCollection)
            {
                if (ignoreTypesPredict?.Invoke(descriptor.ServiceType) == true)
                {
                    services.Add(descriptor);
                    continue;
                }

                if (descriptor.ServiceType.IsSealed ||
                    descriptor.ImplementType.IsProxyType() ||
                    (descriptor.ServiceType.IsClass && (descriptor.ImplementType?.IsSealed == true || descriptor.ImplementType?.IsPublic == false)))
                {
                    services.Add(descriptor);
                }
                else
                {
                    Func <IServiceProvider, object> serviceFactory = null;

                    if (descriptor.ImplementationInstance != null)
                    {
                        serviceFactory = provider => provider.ResolveRequiredService <IProxyFactory>()
                                         .CreateProxyWithTarget(descriptor.ServiceType, descriptor.ImplementationInstance);
                    }
                    else if (descriptor.ImplementationFactory != null)
                    {
                        serviceFactory = provider =>
                        {
                            var implement = descriptor.ImplementationFactory(provider);
                            if (implement?.GetType().IsProxyType() == true)
                            {
                                return(implement);
                            }
                            return(provider.ResolveRequiredService <IProxyFactory>()
                                   .CreateProxyWithTarget(descriptor.ServiceType, implement));
                        };
                    }
                    else if (descriptor.ImplementType != null)
                    {
                        serviceFactory = provider => provider.ResolveRequiredService <IProxyFactory>()
                                         .CreateProxy(descriptor.ServiceType, descriptor.ImplementType);
                    }

                    if (null != serviceFactory)
                    {
                        services.Add(new ServiceDefinition(descriptor.ServiceType, serviceFactory,
                                                           descriptor.ServiceLifetime));
                    }
                    else
                    {
                        services.Add(descriptor);
                    }
                }
            }

            var container = services.Build();

            DependencyResolver.SetDependencyResolver(container);
            return(container);
        }