Пример #1
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);
        }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #4
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);
        }