示例#1
0
 public void Test_ConventionalRegistrationContext()
 {
     var context = new ConventionalRegistrationContext(
         Assembly.GetExecutingAssembly(),
         LocalIocManager,
         new ConventionalRegistrationConfig());
     Assert.IsNotNull(context);
 }
示例#2
0
 public void Test_FromAssembly()
 {
     var context = new ConventionalRegistrationContext(
         Assembly.GetExecutingAssembly(),
         LocalIocManager,
         new ConventionalRegistrationConfig());
     var descriptor = Classes.FromAssembly(context.Assembly);
     Assert.IsNotNull(descriptor);
 }
示例#3
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();
        }
示例#4
0
 public void Test_BasedOn()
 {
     var context = new ConventionalRegistrationContext(
         Assembly.GetExecutingAssembly(),
         LocalIocManager,
         new ConventionalRegistrationConfig());
     var descriptor = Classes.FromAssembly(context.Assembly).IncludeNonPublicTypes().BasedOn<AbpDbContext>();
     Assert.IsNotNull(descriptor);
     Assert.AreEqual(context.IocManager, LocalIocManager);
     Assert.IsNotNull(context.IocManager.IocContainer);
     Assert.IsNotNull(context.IocManager.IocContainer.Kernel);
 }
示例#5
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));
            }
        }
示例#6
0
 public void Test_LifestyleTransient()
 {
     var context = new ConventionalRegistrationContext(
         Assembly.GetExecutingAssembly(),
         LocalIocManager,
         new ConventionalRegistrationConfig());
     var descriptor =
         Classes.FromAssembly(context.Assembly)
             .IncludeNonPublicTypes()
             .BasedOn<AbpDbContext>()
             .WithServiceSelf()
             .LifestyleTransient();
     Assert.IsNotNull(descriptor);
     Assert.IsNotNull(descriptor.WithService);
 }
示例#7
0
 public void Test_Configure()
 {
     var context = new ConventionalRegistrationContext(
         Assembly.GetExecutingAssembly(),
         LocalIocManager,
         new ConventionalRegistrationConfig());
     var descriptor =
         Classes.FromAssembly(context.Assembly)
             .IncludeNonPublicTypes()
             .BasedOn<AbpDbContext>()
             .WithServiceSelf()
             .LifestyleTransient();
     var result = descriptor.Configure(c => c.DynamicParameters(
         (kernel, dynamicParams) =>
         {
             var connectionString = context.IocManager.GetNameOrConnectionStringOrNull();
             if (!string.IsNullOrWhiteSpace(connectionString))
             {
                 dynamicParams["nameOrConnectionString"] = connectionString;
             }
         }));
     Assert.IsNotNull(result);
 }
示例#8
0
 public void Test_GetNameOrConnectionStringOrNull()
 {
     var context = new ConventionalRegistrationContext(
         Assembly.GetExecutingAssembly(),
         LocalIocManager,
         new ConventionalRegistrationConfig());
     var connectionString = context.IocManager.GetNameOrConnectionStringOrNull();
     Assert.IsNull(connectionString);
 }
示例#9
0
 private static BasedOnDescriptor BasedOnDescriptor(ConventionalRegistrationContext context)
 {
     var descriptor =
         Classes.FromAssembly(context.Assembly)
             .IncludeNonPublicTypes()
             .BasedOn<AbpDbContext>()
             .WithServiceSelf()
             .LifestyleTransient();
     var result = descriptor.Configure(c => c.DynamicParameters(
         (kernel, dynamicParams) =>
         {
             var connectionString = context.IocManager.GetNameOrConnectionStringOrNull();
             if (!string.IsNullOrWhiteSpace(connectionString))
             {
                 dynamicParams["nameOrConnectionString"] = connectionString;
             }
         }));
     return result;
 }
示例#10
0
 public void Test_KernelRegister4()
 {
     var context = new ConventionalRegistrationContext(
         Assembly.GetExecutingAssembly(),
         LocalIocManager,
         new ConventionalRegistrationConfig());
     var descriptor = Classes.FromAssembly(context.Assembly);
     var result = descriptor.BasedOn(new List<Type> {typeof(AbpDbContext)});
     var kernel = new DefaultKernel();
     ((IRegistration)result).Register(kernel);
 }
示例#11
0
 public void Test_KernelRegister3()
 {
     var context = new ConventionalRegistrationContext(
         Assembly.GetExecutingAssembly(),
         LocalIocManager,
         new ConventionalRegistrationConfig());
     var result = BasedOnDescriptor(context);
     var kernel = new DefaultKernel();
     ((IRegistration) result).Register(kernel);
 }
示例#12
0
 public void Test_KernelRegister()
 {
     var context = new ConventionalRegistrationContext(
         Assembly.GetExecutingAssembly(),
         LocalIocManager,
         new ConventionalRegistrationConfig());
     var result = BasedOnDescriptor(context);
     var kernel = context.IocManager.IocContainer.Kernel;
     kernel.Register(result);
 }