Ejemplo n.º 1
0
        /// <summary>
        /// Registers <see cref="KeyedServiceCollection{TService, TKey}"/> instance in <see cref="IServiceCollection"/> as singleton.
        /// </summary>
        /// <typeparam name="TService">Implementation contract of registrations kept in collection</typeparam>
        /// <typeparam name="TKey">Type of key we will distinguish implementations with</typeparam>
        /// <param name="services">Services collection</param>
        /// <returns>Registered instance</returns>
        public static KeyedServiceCollection <TService, TKey> AddKeyedServiceCollection <TService, TKey>(
            this IServiceCollection services)
        {
            var collection = new KeyedServiceCollection <TService, TKey>();

            services.AddSingleton(collection);
            return(collection);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Creates instance <see cref="KeyedServiceCollection{TService, TKey}"/>.
 /// This is generaly meant to be used with <see cref="IServiceProvider"/>.
 /// The <see cref="KeyedServiceProvider{TService, TKey}"/> instance is registered with the first service of <see cref="TService"/> inside <see cref="KeyedServiceProviderExtensions.AddKeyedService{TService, TImplementation, TKey}(Extensions.DependencyInjection.IServiceCollection, TKey, Action{Extensions.DependencyInjection.IServiceCollection})"/>.
 /// </summary>
 /// <param name="serviceProvider">Instance of <see cref="IServiceProvider"/>. The <see cref="IServiceProvider"/> can injects itself.</param>
 /// <param name="implementations">Registered implementations collection under the same contract. The collection should be registered in services collection as singleton. You can do that with <see cref="KeyedServiceProviderExtensions.AddKeyedServiceCollection{TService, TKey}(Extensions.DependencyInjection.IServiceCollection)"/> extension.</param>
 public KeyedServiceProvider(IServiceProvider serviceProvider, KeyedServiceCollection <TService, TKey> implementations)
 {
     _lookUp = key =>
     {
         Type resolvedServiceType = implementations[key];
         return((TService)serviceProvider.GetService(resolvedServiceType));
     };
 }