Exemple #1
0
        /// <summary>
        /// 自动根据 descriptor 在注册服务时进行判断是否已经存在指定接口的服务,如果存在会跳过添加,并且可以对注册位置控制。
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="descriptor">要注册的 <see cref="ServiceDescriptor"/> 对象。</param>
        public static void AddSmart(this IServiceCollection collection, SmartServiceDescriptor descriptor)
        {
            if (descriptor == null)
            {
                return;
            }
            switch (descriptor.Options)
            {
            case SmartOptions.Append:
                collection.Add(descriptor);
                break;

            case SmartOptions.TryAdd:
                collection.TryAdd(descriptor);
                break;

            case SmartOptions.Insert:
                collection.Insert(0, descriptor);
                break;

            case SmartOptions.TryAppend:
                collection.TryAddEnumerable(descriptor);
                break;

            default:
                collection.Replace(descriptor);
                break;
            }
        }
Exemple #2
0
        public static SmartServiceDescriptor Create(ServiceDescriptor descriptor, SmartOptions options = SmartOptions.TryAdd)
        {
            Guard.ArgumentNotNull(descriptor, nameof(descriptor));

            SmartServiceDescriptor rd = null;

            if (descriptor.ImplementationInstance != null)
            {
                rd = new SmartServiceDescriptor(descriptor.ServiceType, descriptor.ImplementationInstance)
                {
                    Options = options
                };
            }
            else if (descriptor.ImplementationFactory != null)
            {
                rd = new SmartServiceDescriptor(descriptor.ServiceType, descriptor.ImplementationFactory, descriptor.Lifetime)
                {
                    Options = options
                };
            }
            else if (descriptor.ImplementationType != null)
            {
                rd = new SmartServiceDescriptor(descriptor.ServiceType, descriptor.ImplementationType, descriptor.Lifetime)
                {
                    Options = options
                };
            }
            return(rd);
        }
Exemple #3
0
 /// <summary>
 /// 将 <see cref="ServiceDescriptor"/> 视为可替换的服务,
 /// 在添加服务时自动使用 ServiceCollectionExtensions.TryAdd 方法。
 /// </summary>
 /// <param name="descriptor"></param>
 /// <returns></returns>
 public static ServiceDescriptor AsSmart(this ServiceDescriptor descriptor)
 {
     return(SmartServiceDescriptor.Create(descriptor));
 }