Exemplo n.º 1
0
 /// <summary>
 /// 以类型实现的接口进行服务添加,需排除
 /// <see cref="ITransientDependency"/>、
 /// <see cref="IScopeDependency"/>、
 /// <see cref="ISingletonDependency"/>、
 /// <see cref="IDependency"/>、
 /// <see cref="IDisposable"/>等非业务接口,如无接口则注册自身
 /// </summary>
 /// <param name="services">服务映射信息集合</param>
 /// <param name="implementationTypes">要注册的实现类型集合</param>
 /// <param name="lifetime">注册的生命周期类型</param>
 protected virtual void AddTypeWithInterfaces(IServiceCollection services, Type[] implementationTypes, LifetimeStyle lifetime)
 {
     foreach (Type implementationType in implementationTypes)
     {
         if (implementationType.IsAbstract || implementationType.IsInterface)
         {
             continue;
         }
         Type[] interfaceTypes = GetImplementedInterfaces(implementationType);
         if (interfaceTypes.Length == 0)
         {
             services.Add(implementationType, implementationType, lifetime);
             continue;
         }
         foreach (Type interfaceType in interfaceTypes)
         {
             services.Add(interfaceType, implementationType, lifetime);
         }
     }
 }
Exemplo n.º 2
0
 private static ServiceDescriptor Describe(Type serviceType, Type implementationType, LifetimeStyle lifetime)
 {
     return(new ServiceDescriptor(serviceType, implementationType, lifetime));
 }
Exemplo n.º 3
0
 /// <summary>
 /// 以类型实现的接口进行服务添加,需排除
 /// <see cref="ITransientDependency"/>、
 /// <see cref="IScopeDependency"/>、
 /// <see cref="ISingletonDependency"/>、
 /// <see cref="IDependency"/>、
 /// <see cref="IDisposable"/>等非业务接口,如无接口则注册自身
 /// </summary>
 /// <param name="services">服务映射信息集合</param>
 /// <param name="implementationTypes">要注册的实现类型集合</param>
 /// <param name="lifetime">注册的生命周期类型</param>
 protected virtual void AddTypeWithInterfaces(IServiceCollection services, Type[] implementationTypes, LifetimeStyle lifetime)
 {
     foreach (Type implementationType in implementationTypes)
     {
         if (implementationType.IsAbstract || implementationType.IsInterface)
         {
             continue;
         }
         Type[] interfaceTypes = GetImplementedInterfaces(implementationType);
         if (interfaceTypes.Length == 0)
         {
             services.Add(implementationType, implementationType, lifetime);
             continue;
         }
         foreach (Type interfaceType in interfaceTypes)
         {
             services.Add(interfaceType, implementationType, lifetime);
         }
     }
 }
Exemplo n.º 4
0
        private static IRegistrationBuilder <object, T, TU> ConfigureLifetimeStyle <T, TU>(this IRegistrationBuilder <object, T, TU> builder,
                                                                                           LifetimeStyle lifetime)
        {
            switch (lifetime)
            {
            case LifetimeStyle.Transient:
                builder.InstancePerDependency();
                break;

            case LifetimeStyle.Scoped:
                builder.InstancePerLifetimeScope();
                break;

            case LifetimeStyle.Singleton:
                builder.SingleInstance();
                break;
            }
            return(builder);
        }
Exemplo n.º 5
0
 public virtual void RegisterType(Type type, LifetimeStyle lifetime)
 {
     this.RegisterType(type, (string)null, lifetime);
 }
Exemplo n.º 6
0
 private static ServiceDescriptor Describe <TService, TImplementation>(LifetimeStyle lifetime)
     where TService : class
     where TImplementation : class, TService
 {
     return(Describe(typeof(TService), typeof(TImplementation), lifetime));
 }
Exemplo n.º 7
0
        /// <summary>
        /// 注册指定生命周期类型的映射信息
        /// </summary>
        /// <param name="collection">服务映射信息集合</param>
        /// <param name="serviceType">服务类型</param>
        /// <param name="implementationType">服务实现类型</param>
        /// <param name="lifetime">生命周期类型</param>
        /// <returns></returns>
        public static IServiceCollection Add(this IServiceCollection collection, Type serviceType, Type implementationType, LifetimeStyle lifetime)
        {
            ServiceDescriptor descriptor = new ServiceDescriptor(serviceType, implementationType, lifetime);

            return(collection.TryAdd(descriptor));
        }
Exemplo n.º 8
0
 /// <summary>
 /// 初始化一个<see cref="ServiceDescriptor"/>类型的新实例
 /// </summary>
 public ServiceDescriptor(Type serviceType, Func <IServiceProvider, object> factory, LifetimeStyle lifetime)
     : this(serviceType, lifetime)
 {
     ImplementationFactory = factory;
 }
Exemplo n.º 9
0
 public static void RegisterType <TService>(this IObjectContainer objContainer, LifetimeStyle iLifetimeStyle) where TService : class
 {
     objContainer.Register(f => f.RegisterType <TService>().Lifetime(iLifetimeStyle));
 }
Exemplo n.º 10
0
 public static void RegisterType(this IObjectContainer objContainer, Type objServiceType, Type objComponentType, string strName, LifetimeStyle iLifetimeStyle)
 {
     objContainer.Register(f => f.RegisterType(objServiceType, objComponentType).Named(strName, objServiceType).Lifetime(iLifetimeStyle));
 }
Exemplo n.º 11
0
 public static void RegisterGeneric(this IObjectContainer objContainer, Type objService, Type objComponent, LifetimeStyle iLifetimeStyle)
 {
     objContainer.Register(f => f.RegisterGeneric(objService, objComponent).Lifetime(iLifetimeStyle));
 }
Exemplo n.º 12
0
 public abstract void RegisterType(Type from, Type to, string name, LifetimeStyle lifetime);
Exemplo n.º 13
0
 public virtual void RegisterType(Type from, Type to, LifetimeStyle lifetime)
 {
     this.RegisterType(from, to, (string)null, lifetime);
 }
Exemplo n.º 14
0
 public virtual void RegisterType(Type type, string name, LifetimeStyle lifetime)
 {
     this.RegisterType(type, type, name, lifetime);
 }
Exemplo n.º 15
0
 /// <summary>
 /// 初始化一个<see cref="ServiceDescriptor"/>类型的新实例
 /// </summary>
 public ServiceDescriptor(Type serviceType, Type implementationType, LifetimeStyle lifetime)
     : this(serviceType, lifetime)
 {
     ImplementationType = implementationType;
 }
Exemplo n.º 16
0
 private static ServiceDescriptor Describe(Type serviceType, Func <IServiceProvider, object> factory, LifetimeStyle lifetime)
 {
     return(new ServiceDescriptor(serviceType, factory, lifetime));
 }
Exemplo n.º 17
0
 public static void RegisterType <TService, TComponent>(this IObjectContainer objContainer, string strName, LifetimeStyle iLifetimeStyle)
     where TService : class
     where TComponent : class, TService
 {
     objContainer.Register(f => f.RegisterType <TService, TComponent>().Named <TService>(strName).Lifetime(iLifetimeStyle));
 }
Exemplo n.º 18
0
 /// <summary>
 /// 初始化一个<see cref="ServiceDescriptor"/>类型的新实例
 /// </summary>
 public ServiceDescriptor(Type serviceType, LifetimeStyle lifetime)
 {
     Lifetime    = lifetime;
     ServiceType = serviceType;
 }
Exemplo n.º 19
0
 public static void RegisterInstance <TService>(this IObjectContainer objContainer, TService objInstance, string strName, LifetimeStyle iLifetimeStyle) where TService : class
 {
     objContainer.Register(f => f.RegisterType <TService>(objInstance).Named <TService>(strName).Lifetime(iLifetimeStyle));
 }
Exemplo n.º 20
0
        private static IServiceCollection Add(IServiceCollection collection, Type serviceType, Func <IServiceProvider, object> factory, LifetimeStyle lifetime)
        {
            ServiceDescriptor descriptor = new ServiceDescriptor(serviceType, factory, lifetime);

            return(collection.TryAdd(descriptor));
        }
Exemplo n.º 21
0
 /// <summary>
 /// 将实施类型注册为服务实现.
 /// </summary>
 /// <param name="serviceType">服务类型.</param>
 /// <param name="implementationType">实现类型.</param>
 /// <param name="serviceName">服务名.</param>
 /// <param name="life">实现类型的生命周期.</param>
 public void Register(Type serviceType, Type implementationType, string serviceName = null, LifetimeStyle life = LifetimeStyle.Singleton)
 {
     if (implementationType.IsGenericType)
     {
         var registrationBuilder = _containerBuilder.RegisterGeneric(implementationType).As(serviceType);
         if (serviceName != null)
         {
             registrationBuilder.Named(serviceName, implementationType);
         }
         if (life == LifetimeStyle.Singleton)
         {
             registrationBuilder.SingleInstance();
         }
     }
     else
     {
         var registrationBuilder = _containerBuilder.RegisterType(implementationType).As(serviceType);
         if (serviceName != null)
         {
             registrationBuilder.Named(serviceName, serviceType);
         }
         if (life == LifetimeStyle.Singleton)
         {
             registrationBuilder.SingleInstance();
         }
     }
 }
Exemplo n.º 22
0
 public void Register <T>(string serviceName = null, LifetimeStyle lifeStyle = LifetimeStyle.Singleton) where T : class
 {
     Register(typeof(T), serviceName, lifeStyle);
 }
Exemplo n.º 23
0
 public LifeCycleAttribute(LifetimeStyle lifetime)
 {
     this.Lifetime = lifetime;
 }