Exemplo n.º 1
0
        /// <summary>
        /// Registers types of given assembly by all conventional registrars. See <see cref="AddConventionalRegistrar"/> method.
        /// </summary>
        /// <param name="assembly">Assembly to register</param>
        /// <param name="config">Additional configuration</param>
        public void RegisterAssemblyByConvention(Assembly assembly, ConventionalRegistrationConfig config)
        {
            var context = new ConventionalRegistrationContext(assembly, this, config);

            foreach (var registerer in _conventionalRegistrars)
            {
                registerer.RegisterAssembly(context);
            }

            if (config.InstallInstallers)
            {
                this.Install(assembly);
            }

            // 这里使用 TPL 并行库的原因是因为存在大量仓储类型与应用服务需要注册,应最大限度利用 CPU 来进行操作
            Parallel.ForEach(_waitRegisterInterceptor, keyValue =>
            {
                var proxyBuilder = new DefaultProxyBuilder();

                Type proxyType;
                if (keyValue.Key.IsInterface)
                {
                    proxyType = proxyBuilder.CreateInterfaceProxyTypeWithTargetInterface(keyValue.Key, ArrayTools.Empty <Type>(), ProxyGenerationOptions.Default);
                }
                else if (keyValue.Key.IsClass())
                {
                    proxyType = proxyBuilder.CreateClassProxyTypeWithTarget(keyValue.Key, ArrayTools.Empty <Type>(), ProxyGenerationOptions.Default);
                }
                else
                {
                    throw new ArgumentException($"类型 {keyValue.Value} 不支持进行拦截器服务集成。");
                }

                var decoratorSetup = Setup.DecoratorWith(useDecorateeReuse: true);

                // 使用 ProxyBuilder 创建好的代理类替换原有类型的实现
                IocContainer.Register(keyValue.Key, proxyType,
                                      made: Made.Of(type => type.GetConstructors().SingleOrDefault(c => c.GetParameters().Length != 0),
                                                    Parameters.Of.Type <IInterceptor[]>(request =>
                {
                    var objects = new List <object>();
                    foreach (var interceptor in keyValue.Value)
                    {
                        objects.Add(request.Container.Resolve(interceptor));
                    }

                    return(objects.Cast <IInterceptor>().ToArray());
                }),
                                                    PropertiesAndFields.Auto),
                                      setup: decoratorSetup);
            });

            _waitRegisterInterceptor.Clear();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Registers types of given assembly by all conventional registrars. See <see cref="AddConventionalRegistrar"/> method.
        /// </summary>
        /// <param name="assembly">Assembly to register</param>
        /// <param name="config">Additional configuration</param>
        public void RegisterAssemblyByConvention(Assembly assembly, ConventionalRegistrationConfig config)
        {
            var context = new ConventionalRegistrationContext(assembly, this, config);

            foreach (var registerer in _conventionalRegistrars)
            {
                registerer.RegisterAssembly(context);
            }

            if (config.InstallInstallers)
            {
                IocContainer.Install(FromAssembly.Instance(assembly));
            }
        }
 internal ConventionalRegistrationContext(Assembly assembly, IIocManager iocManager, ConventionalRegistrationConfig config)
 {
     Assembly   = assembly;
     IocManager = iocManager;
     Config     = config;
 }
 internal ConventionalRegistrationContext(Assembly assembly, IIocManager iocManager, ConventionalRegistrationConfig config)
 {
     Assembly = assembly;
     IocManager = iocManager;
     Config = config;
 }