/// <summary>
        /// Get service of type <typeparamref name="T"/> from the <see cref="IServiceProvider"/>.
        /// </summary>
        /// <typeparam name="TInterface">The type of service object to get.</typeparam>
        /// <param name="serviceProvider">The <see cref="IServiceProvider"/> to retrieve the service object from.</param>
        /// <param name="dependencyName">Dependency name</param>
        /// <returns>A service object of type <typeparamref name="TInterface"/>.</returns>
        public static TInterface GetService <TInterface>(
            this IServiceProvider serviceProvider, string dependencyName)
        {
            var dependencyType = DependencyDictionary.GetTypeByName <TInterface>(dependencyName);

            return((TInterface)serviceProvider.GetService(dependencyType));
        }
Example #2
0
        /// <summary>
        /// Adds a singleton service of the type specified in <typeparamref name="TService"/>  using the
        /// dependency name specified in <paramref name="dependencyName"/> to the
        /// specified <see cref="IServiceCollection"/>
        /// </summary>
        /// <typeparam name="TService">The type of the service to add.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <param name="dependencyName">Dependency name.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        /// <seealso cref="ServiceLifetime.Singleton"/>
        public static IServiceCollection AddSingleton <TService>(
            this IServiceCollection services, string dependencyName)
            where TService : class
        {
            DependencyDictionary.Register <TService, TService>(dependencyName);
            services.AddSingleton <TService>();

            return(services);
        }
Example #3
0
        /// <summary>
        /// Adds a singleton service of the type specified in <typeparamref name="TInterface"/> with an
        /// implementation type specified in <typeparamref name="TImplementation" /> using the
        /// dependency name specified in <paramref name="dependencyName"/> to the
        /// specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TInterface">The type of the service to add.</typeparam>
        /// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <param name="dependencyName">Dependency name.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        /// <seealso cref="ServiceLifetime.Singleton"/>
        public static IServiceCollection AddSingleton <TInterface, TImplementation>(
            this IServiceCollection services, string dependencyName)
            where TImplementation : class, TInterface
        {
            DependencyDictionary.Register <TInterface, TImplementation>(dependencyName);
            services.AddSingleton <TImplementation>();

            return(services);
        }
Example #4
0
        /// <summary>
        /// Adds a singleton service of the type specified in <typeparamref name="TService"/>  using the
        /// dependency name specified in <paramref name="dependencyName"/> to the
        /// specified <see cref="IServiceCollection"/>
        /// </summary>
        /// <typeparam name="TService">The type of the service to add.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <param name="dependencyName">Dependency name.</param>
        /// <param name="implementationFactory">The factory that creates the service.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        /// <seealso cref="ServiceLifetime.Singleton"/>
        public static IServiceCollection AddSingleton <TService>(
            this IServiceCollection services,
            Func <IServiceProvider, TService> implementationFactory,
            string dependencyName)
            where TService : class
        {
            DependencyDictionary.Register <TService, TService>(dependencyName);
            services.AddSingleton(implementationFactory);

            return(services);
        }
Example #5
0
        /// <summary>
        /// Adds a transient service of the type specified in <typeparamref name="TInterface"/> with an
        /// implementation type specified in <typeparamref name="TImplementation" /> using the
        /// dependency name specified in <paramref name="dependencyName"/> to the
        /// specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TInterface">The type of the service to add.</typeparam>
        /// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <param name="implementationFactory">The factory that creates the service.</param>
        /// <param name="dependencyName">Dependency name.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        /// <seealso cref="ServiceLifetime.Transient"/>
        public static IServiceCollection AddTransient <TInterface, TImplementation>(
            this IServiceCollection services,
            Func <IServiceProvider, TImplementation> implementationFactory,
            string dependencyName)
            where TInterface : class
            where TImplementation : class, TInterface
        {
            DependencyDictionary.Register <TInterface, TImplementation>(dependencyName);
            services.AddTransient(implementationFactory);

            return(services);
        }
Example #6
0
        /// <summary>
        /// Clears list of all named registrations in the specified <see cref="IServiceCollection"/>
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> to clear named registrations from.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection ClearNamedRegistrations(this IServiceCollection services)
        {
            DependencyDictionary.Clear();

            return(services);
        }