private void Initialize_Test(DependencyLifeStyle lifeStyle)
        {
            MyClass1.CreateCount = 0;
            MyClass2.CreateCount = 0;
            MyClass3.CreateCount = 0;

            LocalIocManager.Register<MyClass1>(lifeStyle);
            LocalIocManager.Register<MyClass2>(lifeStyle);
            LocalIocManager.Register<MyClass3>(lifeStyle);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Registers a type with it's implementation.
 /// </summary>
 /// <typeparam name="TType">Registering type</typeparam>
 /// <typeparam name="TImpl">The type that implements <see cref="TType"/></typeparam>
 /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
 public void Register <TType, TImpl>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
     where TType : class
     where TImpl : class, TType
 {
     IocContainer.Register(ApplyLifestyle(Component.For <TType, TImpl>().ImplementedBy <TImpl>(), lifeStyle));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Registers a type as self registration.
 /// </summary>
 /// <typeparam name="TType">Type of the class</typeparam>
 /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
 public void Register <TType>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton) where TType : class
 {
     IocContainer.Register(ApplyLifestyle(Component.For <TType>(), lifeStyle));
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Registers a type with it's implementation.
 /// </summary>
 /// <param name="type">Type of the class</param>
 /// <param name="impl">The type that implements <paramref name="type"/></param>
 /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
 public void Register(Type type, Type impl, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
 {
     IocContainer.Register(ApplyLifestyle(Component.For(type, impl).ImplementedBy(impl), lifeStyle));
 }
 /// <summary>
 /// Used to replace a service type.
 /// 用于替换一个服务类型
 /// </summary>
 /// <param name="configuration">The configuration. / ABP启动配置</param>
 /// <param name="type">Type. / 类型</param>
 /// <param name="impl">Implementation. / 类型实现</param>
 /// <param name="lifeStyle">Life style. / 生命周期</param>
 public static void ReplaceService(this IAbpStartupConfiguration configuration, Type type, Type impl, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
 {
     configuration.ReplaceService(type, () =>
     {
         configuration.IocManager.Register(type, impl, lifeStyle);
     });
 }
Ejemplo n.º 6
0
        private static void AddTypes(IServiceCollection services, TypeFinder finder, Type findType, DependencyLifeStyle life)
        {
            var a = FindClassesOfType(findType, finder);

            foreach (var type in a)
            {
                var implementedInterfaces = type.GetTypeInfo().ImplementedInterfaces.Where(t => t != findType).ToList();
                if (implementedInterfaces.Any())
                {
                    foreach (var implementedInterface in implementedInterfaces)
                    {
                        switch (life)
                        {
                        case DependencyLifeStyle.Transient:
                            services.AddTransient(implementedInterface, type);
                            break;

                        case DependencyLifeStyle.Scoped:
                            services.AddScoped(implementedInterface, type);
                            break;

                        case DependencyLifeStyle.Singleton:
                            services.AddSingleton(implementedInterface, type);
                            break;

                        default:
                            services.AddTransient(implementedInterface, type);
                            break;
                        }
                    }
                }
                switch (life)
                {
                case DependencyLifeStyle.Transient:
                    services.AddTransient(type, type);
                    break;

                case DependencyLifeStyle.Scoped:
                    services.AddScoped(type, type);
                    break;

                case DependencyLifeStyle.Singleton:
                    services.AddSingleton(type, type);
                    break;

                default:
                    services.AddTransient(type, type);
                    break;
                }
            }
        }
        /// <summary>
        /// Set the lifestyle of the registered builder
        /// </summary>
        private ComponentRegistration <T> ApplyLifestyle <T>(ComponentRegistration <T> registration, DependencyLifeStyle lifeStyle)
            where T : class
        {
            switch (lifeStyle)
            {
            case DependencyLifeStyle.Transient:
                return(registration.LifestyleTransient());

            case DependencyLifeStyle.Scoped:
                return(registration.LifestyleScoped());

            case DependencyLifeStyle.Singleton:
                return(registration.LifestyleSingleton());

            default:
                throw new ArgumentException(nameof(lifeStyle));
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Registers a type with it's implementation if it's not registered before.
        /// </summary>
        /// <typeparam name="TType">Registering type</typeparam>
        /// <typeparam name="TImpl">The type that implements <see cref="TType"/></typeparam>
        /// <param name="iocRegistrar">Registrar</param>
        /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
        /// <returns>True, if registered for given implementation.</returns>
        public static bool RegisterIfNot <TType, TImpl>(this IIocRegistrar iocRegistrar, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
            where TType : class
            where TImpl : class, TType
        {
            if (iocRegistrar.IsRegistered <TType>())
            {
                return(false);
            }

            iocRegistrar.Register <TType, TImpl>(lifeStyle);
            return(true);
        }
        public void Register <TImplementation>(DependencyLifeStyle dependencyLifeStyle) where TImplementation : class
        {
            var lifetimeManager = LifetimeMap(dependencyLifeStyle);

            _container.RegisterType <TImplementation>(lifetimeManager);
        }
Ejemplo n.º 10
0
 public void Register <TType, TImpl>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
 {
     IocManager.Register(typeof(TType), typeof(TImpl), lifeStyle);
 }
Ejemplo n.º 11
0
 public void Register <T>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton) where T : class
 {
     IocManager.Register(typeof(T), typeof(T), lifeStyle);
 }
Ejemplo n.º 12
0
 protected void Register <TService, TImplementation>(TService instance, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton) where TImplementation : class, TService where TService : class
 {
     LocalIocManager.IocContainer.Register(
         Component.For <TService>().ImplementedBy <TImplementation>().Instance(instance).ApplyLifeStyle(lifeStyle)
         );
 }
Ejemplo n.º 13
0
 protected void Register <TService, TImplementation>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton) where TImplementation : class, TService where TService : class
 {
     LocalIocManager.Register <TService, TImplementation>();
 }
Ejemplo n.º 14
0
 protected void Register <T>(T instance, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton) where T : class
 {
     LocalIocManager.IocContainer.Register(
         Component.For <T>().Instance(instance).ApplyLifeStyle(lifeStyle)
         );
 }
Ejemplo n.º 15
0
 protected void Register <T>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton) where T : class
 {
     LocalIocManager.Register <T>(lifeStyle);
 }
Ejemplo n.º 16
0
        private IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> ApplyLifeStyle <TLimit, TActivatorData, TRegistrationStyle>(IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> chain, DependencyLifeStyle lifeStyle)
        {
            switch (lifeStyle)
            {
            case DependencyLifeStyle.Singleton:
                chain = chain.SingleInstance();
                break;

            case DependencyLifeStyle.Transient:
                chain = chain.InstancePerDependency();
                break;

            default:
                throw new NotSupportedException("not support the style" + lifeStyle.ToString());
            }

            return(chain);
        }
 public void RegisterAndDecorate <TInterface, TDecorated, TDecorator>(DependencyLifeStyle dependencyLifeStyle)
     where TInterface : class where TDecorated : TInterface where TDecorator : TInterface
 {
     _container.RegisterType <TInterface, TDecorator>(LifetimeMap(dependencyLifeStyle));
     _container.RegisterType <TInterface, TDecorated>(LifetimeMap(dependencyLifeStyle));
 }
Ejemplo n.º 18
0
        /// <summary>
        /// Registers a type with it's implementation if it's not registered before.
        /// </summary>
        /// <param name="iocRegistrar">Registrar</param>
        /// <param name="type">Type of the class</param>
        /// <param name="impl">The type that implements <paramref name="type"/></param>
        /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
        /// <returns>True, if registered for given implementation.</returns>
        public static bool RegisterIfNot(this IIocRegistrar iocRegistrar, Type type, Type impl, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
        {
            if (iocRegistrar.IsRegistered(type))
            {
                return(false);
            }

            iocRegistrar.Register(type, impl, lifeStyle);
            return(true);
        }
Ejemplo n.º 19
0
 /// <summary>
 /// Registers a type as self registration.
 /// </summary>
 /// <typeparam name="TType">Type of the class</typeparam>
 /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
 public void Register <TType>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton) where TType : class
 {
     IocContainer.RegisterType <TType>(GetLifetimeManager(lifeStyle));
 }
 /// <inheritdoc />
 public override void Register(Type serviceType, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
 {
     _container.Register(ApplyLifestyle(Component.For(serviceType), lifeStyle));
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Registers a type as self registration.
 /// </summary>
 /// <param name="type">Type of the class</param>
 /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
 public void Register(Type type, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
 {
     IocContainer.RegisterType(type, GetLifetimeManager(lifeStyle));
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Used to replace a service type.
 /// </summary>
 /// <typeparam name="TType">Type of the service.</typeparam>
 /// <typeparam name="TImpl">Type of the implementation.</typeparam>
 /// <param name="configuration">The configuration.</param>
 /// <param name="lifeStyle">Life style.</param>
 public static void ReplaceService <TType, TImpl>(this IAbpStartupConfiguration configuration, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
     where TType : class
     where TImpl : class, TType
 {
     configuration.ReplaceService(typeof(TType), () =>
     {
         //官网说的是先注册的作为实际绑定。那么这里再次注册为什么能替换掉之前的呢?也没有调用用IsDefault()
         //解释:在AbpKernelModule.Initialize方法中首先调用的就是Replace。所以他们是最先被注册的,也就是最终使用的注入
         configuration.IocManager.Register <TType, TImpl>(lifeStyle);
     });
 }
 /// <summary>
 /// Register an instance obtained through the factory to service generics
 /// </summary>
 public abstract void Register <TService>(
     Func <IIocResolver, TService> implementationFactory,
     DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
     where TService : class;
 /// <summary>
 /// Used to replace a service type.
 /// 用于替换一个服务类型
 /// </summary>
 /// <typeparam name="TType">Type of the service. / 服务类型</typeparam>
 /// <typeparam name="TImpl">Type of the implementation. / 类型的实现</typeparam>
 /// <param name="configuration">The configuration. / ABP启动配置</param>
 /// <param name="lifeStyle">Life style. / 生命周期</param>
 public static void ReplaceService <TType, TImpl>(this IAbpStartupConfiguration configuration, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
     where TType : class
     where TImpl : class, TType
 {
     configuration.ReplaceService(typeof(TType), () =>
     {
         configuration.IocManager.Register <TType, TImpl>(lifeStyle);
     });
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Register service generic
 /// </summary>
 public virtual void Register <TService>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
     where TService : class
 {
     Register(typeof(TService), lifeStyle: lifeStyle);
 }
Ejemplo n.º 26
0
        private static ComponentRegistration <T> ApplyLifestyle <T>(ComponentRegistration <T> registration, DependencyLifeStyle lifeStyle)
            where T : class
        {
            switch (lifeStyle)
            {
            case DependencyLifeStyle.Transient:
                return(registration.LifestyleTransient());

            case DependencyLifeStyle.Singleton:
                return(registration.LifestyleSingleton());

            default:
                return(registration);
            }
        }
Ejemplo n.º 27
0
 /// <summary>
 /// Register an instance obtained through the factory to service type
 /// </summary>
 public abstract void Register(
     Type serviceType,
     Func <IIocResolver, object> implementationFactory,
     DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton);
Ejemplo n.º 28
0
 /// <summary>
 /// Registers a type as self registration.
 /// </summary>
 /// <param name="type">Type of the class</param>
 /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
 public void Register(Type type, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
 {
     IocContainer.Register(ApplyLifestyle(Component.For(type), lifeStyle));
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Register implementation to service interfaces
 /// </summary>
 public virtual void Register <TService, TImplementation>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
     where TService : class
     where TImplementation : class, TService
 {
     Register(typeof(TService), typeof(TImplementation), lifeStyle: lifeStyle);
 }
Ejemplo n.º 30
0
 /// <summary>
 /// Register implementation to service interfaces
 /// </summary>
 public abstract void Register(
     Type serviceType,
     Type implementationType,
     DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton);
Ejemplo n.º 31
0
        /// <summary>
        ///     Registers a type with it's implementation if it's not registered before.
        /// </summary>
        /// <param name="iocRegistrar">Registrar</param>
        /// <param name="type">Type of the class</param>
        /// <param name="impl">The type that implements <paramref name="type" /></param>
        /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
        public static void RegisterIfAbsent(this IIocRegistrar iocRegistrar, Type type, Type impl, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
        {
            if (iocRegistrar.IsRegistered(type))
            {
                return;
            }

            iocRegistrar.Register(type, impl, lifeStyle);
        }