/// <summary> /// Adds a service contributor, which has a chance to chain previously registered service instances. /// </summary> /// <typeparam name="T">The service type.</typeparam> /// <param name="obj">The <see cref="IServiceCollection"/>.</param> /// <param name="contributor">An instance of <see cref="ApiServiceContributor{T}"/>.</param> /// <returns>Current <see cref="IServiceCollection"/></returns> public static IServiceCollection AddContributor <T>( this IServiceCollection obj, ApiServiceContributor <T> contributor) where T : class { Ensure.NotNull(obj, "obj"); Ensure.NotNull(contributor, "contributor"); return(obj.AddContributorNoCheck <T>(contributor)); }
/// <summary> /// Adds a service contributor, which has a chance to chain previously registered service instances. /// </summary> /// <typeparam name="T">The service type.</typeparam> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="contributor">An instance of <see cref="ApiServiceContributor{T}"/>.</param> /// <returns>Current <see cref="IServiceCollection"/></returns> public static IServiceCollection AddContributor <T>( this IServiceCollection services, ApiServiceContributor <T> contributor) where T : class { Ensure.NotNull(services, "services"); Ensure.NotNull(contributor, "contributor"); return(services.AddContributorNoCheck <T>(contributor)); }
private static IServiceCollection AddContributorNoCheck <TService>( this IServiceCollection services, ApiServiceContributor <TService> contributor) where TService : class { // Services have singleton lifetime by default, call Make... to change. services.TryAddSingleton(typeof(TService), ChainedService <TService> .DefaultFactory); services.AddSingleton(contributor); return(services); }
private static IServiceCollection AddContributorNoCheck <T>( this IServiceCollection obj, ApiServiceContributor <T> contributor) where T : class { // Services have singleton lifetime by default, call Make... to change. obj.TryAddSingleton(typeof(T), ChainedService <T> .DefaultFactory); obj.AddInstance(contributor); return(obj); }
private static IServiceCollection AddContributorNoCheck <TService>( this IServiceCollection services, ApiServiceContributor <TService> contributor, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton) where TService : class { var serviceDescriptor = new ServiceDescriptor(typeof(TService), ChainedService <TService> .DefaultFactory, serviceLifetime); services.TryAdd(serviceDescriptor); services.AddSingleton(contributor); return(services); }