internal void Build(IServiceCollection services) { var descriptors = ServiceKeys.Distinct().Select(serviceKey => { var serviceType = serviceKey.ServiceType; ServiceDescriptor descriptor; if (serviceKey is KeyedServiceKey) { if (ImplementationFactory != null) { descriptor = RabbitServiceDescriptor.Create(serviceType, ImplementationFactory, Lifetime); } else if (ImplementationType != null) { descriptor = RabbitServiceDescriptor.Create(serviceType, ImplementationType, Lifetime); } else { descriptor = RabbitServiceDescriptor.Create(serviceType, ImplementationInstance); } ((RabbitServiceDescriptor)descriptor).ServiceKey = serviceKey; } else { if (ImplementationFactory != null) { descriptor = new ServiceDescriptor(serviceType, ImplementationFactory, Lifetime); } else if (ImplementationType != null) { descriptor = new ServiceDescriptor(serviceType, ImplementationType, Lifetime); } else { descriptor = new ServiceDescriptor(serviceType, ImplementationInstance); } } return(descriptor); }).ToArray(); foreach (var descriptor in descriptors) { services.Add(descriptor); } }
/// <summary> /// Add Accessor to <see cref="IServiceCollection"/> /// </summary> /// <param name="services">Service collection</param> /// <returns>Service Provider</returns> public IServiceCollection AddAccessor(IServiceCollection services) { // Get global values for undefined variables if (ParentAccessorBuilders != null) { if (Lifetime == null) { Lifetime = ParentAccessorBuilders.Lifetime; } if (ResolverType == null) { ResolverType = ParentAccessorBuilders.ResolverType; } if (ServiceKeyResolver == null) { ServiceKeyResolver = ParentAccessorBuilders.ServiceKeyResolver; } } // Apply default Service Key Resolver? if (ServiceKeyResolver == null) { ServiceKeyResolver = (key, keys) => key is IComparable ? keys.Any(i => ((IComparable)key).CompareTo(i) == 0) : keys.Contains(key); } // Validate Accessor Builder if (Lifetime == null) { throw new Exception("Accessor Service Lifetime is not defined."); } if (ResolverType == null) { throw new Exception("Accessor's Resolver type is not defined."); } if (!HasCustomResolver && !ServiceKeys.Any()) { throw new Exception("No Accessor Key(s) defined."); } if (ServiceResolver == null && AsyncServiceResolver == null) { throw new Exception("Neither a synchronous or asynchronous Service Resolver is defined."); } // Define Service Resolvers if (ServiceResolver == null) { ServiceResolver = (p, a) => AsyncHelper.RunSync(() => AsyncServiceResolver(p, a)); } else if (AsyncServiceResolver == null) { AsyncServiceResolver = (p, a) => Task.Factory.StartNew(() => ServiceResolver(p, a)); } // Create Factory Func <IServiceProvider, object> factory; if (ResolverType.Value == Microsoft.Extensions.DependencyInjection.ServiceLifetime.Transient) { factory = p => new TransientServiceAccessor <TService, TKey>(ServiceKeys, p, ServiceResolver, AsyncServiceResolver, ServiceKeyResolver); } else { factory = p => new SingletonServiceAccessor <TService, TKey>(ServiceKeys, p, ServiceResolver, AsyncServiceResolver, ServiceKeyResolver); } // Add Service to Collection services.Add(new ServiceDescriptor(typeof(IServiceAccessor <TService, TKey>), factory, Lifetime.Value)); // Return Collection return(services); }