Dependencyinjection: НуТна ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π›Π΅Π½ΠΈΠ²Ρ‹ΠΉ<t/>

Π‘ΠΎΠ·Π΄Π°Π½Π½Ρ‹ΠΉ Π½Π° 19 Π°Π²Π³. 2014  Β·  3ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ  Β·  Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ: aspnet/DependencyInjection

DI Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Ρ‚ΡŒ Lazy.
ΠœΡ‹ Π½Π΅ Ρ…ΠΎΡ‚ΠΈΠΌ ΠΊΠΎΠ³Π΄Π°-Π½ΠΈΠ±ΡƒΠ΄ΡŒ сразу ΡΠΎΠ·Π΄Π°Π²Π°Ρ‚ΡŒ экзСмпляр.

Π‘Π°ΠΌΡ‹ΠΉ ΠΏΠΎΠ»Π΅Π·Π½Ρ‹ΠΉ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ

МоТно Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ нСсколько ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠ² Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΡ ΠΊ IServiceCollection Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΡƒ Lazy с ΠΎΡ‡Π΅Π½ΡŒ нСбольшими усилиями, Π½Π΅ трСбуя, Ρ‡Ρ‚ΠΎΠ±Ρ‹ всС ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Ρ‹ IoC ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π»ΠΈ эту Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ. ΠœΡ‹ΡΠ»ΠΈ?

namespace Microsoft.Extensions.DependencyInjection
{
    using System;

    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// Adds a lazily initialized scoped service of the type specified in <typeparamref name="TService"/> 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>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddScopedLazy<TService>(this IServiceCollection services)
            where TService : class
        {
            return services
                .AddScoped<TService>()
                .AddScoped(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized scoped service of the type specified in <typeparamref name="TService"/> with a
        /// factory specified in <paramref name="implementationFactory"/> to the specified
        /// <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <param name="implementationFactory">The factory that creates the service.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddScopedLazy<TService>(
            this IServiceCollection services,
            Func<IServiceProvider, TService> implementationFactory)
            where TService : class
        {
            return services
                .AddScoped(implementationFactory)
                .AddScoped(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized scoped service of the type specified in <typeparamref name="TService"/> with an
        /// implementation type specified in <typeparamref name="TImplementation"/> to the specified
        /// <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</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>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddScopedLazy<TService, TImplementation>(this IServiceCollection services)
            where TService : class
            where TImplementation : class, TService
        {
            return services
                .AddScoped<TService, TImplementation>()
                .AddScoped(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized scoped service of the type specified in <typeparamref name="TService"/> with an
        /// implementation type specified in <typeparamref name="TImplementation"/> using the factory specified in
        /// <paramref name="implementationFactory"/> to the specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</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>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddScopedLazy<TService, TImplementation>(
            this IServiceCollection services,
            Func<IServiceProvider, TImplementation> implementationFactory)
            where TService : class
            where TImplementation : class, TService
        {
            return services
                .AddScoped<TService, TImplementation>()
                .AddScoped(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized singleton service of the type specified in <typeparamref name="TService"/> to the
        /// specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddSingletonLazy<TService>(this IServiceCollection services)
            where TService : class
        {
            return services
                .AddSingleton<TService>()
                .AddSingleton(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized singleton service of the type specified in <typeparamref name="TService"/> with a
        /// factory specified in <paramref name="implementationFactory"/> to the specified
        /// <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <param name="implementationFactory">The factory that creates the service.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddSingletonLazy<TService>(
            this IServiceCollection services,
            Func<IServiceProvider, TService> implementationFactory)
            where TService : class
        {
            return services
                .AddSingleton(implementationFactory)
                .AddSingleton(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized singleton service of the type specified in <typeparamref name="TService"/> with
        /// an instance specified in implementationInstance to the specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <param name="implementationInstance">The implementation instance.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddSingletonLazy<TService>(
            this IServiceCollection services,
            TService implementationInstance)
            where TService : class
        {
            return services
                .AddSingleton<TService>()
                .AddSingleton(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized singleton service of the type specified in <typeparamref name="TService"/> with
        /// an implementation type specified in <typeparamref name="TImplementation"/> to the specified
        /// <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</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>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddSingletonLazy<TService, TImplementation>(this IServiceCollection services)
            where TService : class
            where TImplementation : class, TService
        {
            return services
                .AddSingleton<TService, TImplementation>()
                .AddSingleton(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized singleton service of the type specified in <typeparamref name="TService"/> with an implementation
        /// type specified in <typeparamref name="TImplementation"/> using the factory specified in <paramref name="implementationFactory"/>
        /// to the specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</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>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddSingletonLazy<TService, TImplementation>(
            this IServiceCollection services,
            Func<IServiceProvider, TImplementation> implementationFactory)
            where TService : class
            where TImplementation : class, TService
        {
            return services
                .AddSingleton<TService, TImplementation>(implementationFactory)
                .AddSingleton(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized transient service of the type specified in <typeparamref name="TService"/> to the specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddTransientLazy<TService>(this IServiceCollection services)
            where TService : class
        {
            return services
                .AddTransient<TService>()
                .AddTransient(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized transient service of the type specified in <typeparamref name="TService"/> with a factory specified
        /// in <paramref name="implementationFactory"/> to the specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <param name="implementationFactory">The factory that creates the service.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddTransientLazy<TService>(
            this IServiceCollection services,
            Func<IServiceProvider, TService> implementationFactory)
            where TService : class
        {
            return services
                .AddTransient(implementationFactory)
                .AddTransient(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized transient service of the type specified in <typeparamref name="TService"/> with an implementation
        /// type specified in <typeparamref name="TImplementation"/> to the specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</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>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddTransientLazy<TService, TImplementation>(this IServiceCollection services)
            where TService : class
            where TImplementation : class, TService
        {
            return services
                .AddTransient<TService, TImplementation>()
                .AddTransient(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized transient service of the type specified in <typeparamref name="TService"/> with an implementation
        /// type specified in <typeparamref name="TImplementation"/> using the factory specified in <paramref name="implementationFactory"/>
        /// to the specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</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>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddTransientLazy<TService, TImplementation>(
            this IServiceCollection services,
            Func<IServiceProvider, TImplementation> implementationFactory)
            where TService : class
            where TImplementation : class, TService
        {
            return services
                .AddTransient(implementationFactory)
                .AddTransient(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }
    }
}

ВсС 3 ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ

МнС нравится эта идСя, Π½ΠΎ Π½Π΅ всС ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Ρ‹ IoC, Π½Π° ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Π΄ΠΎΠ»ΠΆΠ΅Π½ Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ ASP.NET, ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°ΡŽΡ‚ Π΅Π΅. НапримСр, ΠΏΠΎΡ…ΠΎΠΆΠ΅, Ρ‡Ρ‚ΠΎ эта функция Ρ€Π΅Π°Π»ΠΈΠ·ΠΎΠ²Π°Π½Π° ΠΊΠ°ΠΊ Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠ΅ для Ninject .

Если Π²Ρ‹ Ρ…ΠΎΡ‚ΠΈΡ‚Π΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ Lazy<T> Π² своСм ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠΈ, Π²Ρ‹ ΠΌΠΎΠΆΠ΅Ρ‚Π΅ Π²Ρ‹Π±Ρ€Π°Ρ‚ΡŒ использованиС ΠΏΠΎΠ»Π½ΠΎΡ„ΡƒΠ½ΠΊΡ†ΠΈΠΎΠ½Π°Π»ΡŒΠ½ΠΎΠ³ΠΎ ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Π° IoC, Ρ‚Π°ΠΊΠΎΠ³ΠΎ ΠΊΠ°ΠΊ Autofac ΠΈΠ»ΠΈ Ninject (с Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠ΅ΠΌ).

МоТно Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ нСсколько ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠ² Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΡ ΠΊ IServiceCollection Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΡƒ Lazy с ΠΎΡ‡Π΅Π½ΡŒ нСбольшими усилиями, Π½Π΅ трСбуя, Ρ‡Ρ‚ΠΎΠ±Ρ‹ всС ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Ρ‹ IoC ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π»ΠΈ эту Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ. ΠœΡ‹ΡΠ»ΠΈ?

namespace Microsoft.Extensions.DependencyInjection
{
    using System;

    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// Adds a lazily initialized scoped service of the type specified in <typeparamref name="TService"/> 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>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddScopedLazy<TService>(this IServiceCollection services)
            where TService : class
        {
            return services
                .AddScoped<TService>()
                .AddScoped(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized scoped service of the type specified in <typeparamref name="TService"/> with a
        /// factory specified in <paramref name="implementationFactory"/> to the specified
        /// <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <param name="implementationFactory">The factory that creates the service.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddScopedLazy<TService>(
            this IServiceCollection services,
            Func<IServiceProvider, TService> implementationFactory)
            where TService : class
        {
            return services
                .AddScoped(implementationFactory)
                .AddScoped(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized scoped service of the type specified in <typeparamref name="TService"/> with an
        /// implementation type specified in <typeparamref name="TImplementation"/> to the specified
        /// <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</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>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddScopedLazy<TService, TImplementation>(this IServiceCollection services)
            where TService : class
            where TImplementation : class, TService
        {
            return services
                .AddScoped<TService, TImplementation>()
                .AddScoped(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized scoped service of the type specified in <typeparamref name="TService"/> with an
        /// implementation type specified in <typeparamref name="TImplementation"/> using the factory specified in
        /// <paramref name="implementationFactory"/> to the specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</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>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddScopedLazy<TService, TImplementation>(
            this IServiceCollection services,
            Func<IServiceProvider, TImplementation> implementationFactory)
            where TService : class
            where TImplementation : class, TService
        {
            return services
                .AddScoped<TService, TImplementation>()
                .AddScoped(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized singleton service of the type specified in <typeparamref name="TService"/> to the
        /// specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddSingletonLazy<TService>(this IServiceCollection services)
            where TService : class
        {
            return services
                .AddSingleton<TService>()
                .AddSingleton(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized singleton service of the type specified in <typeparamref name="TService"/> with a
        /// factory specified in <paramref name="implementationFactory"/> to the specified
        /// <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <param name="implementationFactory">The factory that creates the service.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddSingletonLazy<TService>(
            this IServiceCollection services,
            Func<IServiceProvider, TService> implementationFactory)
            where TService : class
        {
            return services
                .AddSingleton(implementationFactory)
                .AddSingleton(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized singleton service of the type specified in <typeparamref name="TService"/> with
        /// an instance specified in implementationInstance to the specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <param name="implementationInstance">The implementation instance.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddSingletonLazy<TService>(
            this IServiceCollection services,
            TService implementationInstance)
            where TService : class
        {
            return services
                .AddSingleton<TService>()
                .AddSingleton(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized singleton service of the type specified in <typeparamref name="TService"/> with
        /// an implementation type specified in <typeparamref name="TImplementation"/> to the specified
        /// <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</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>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddSingletonLazy<TService, TImplementation>(this IServiceCollection services)
            where TService : class
            where TImplementation : class, TService
        {
            return services
                .AddSingleton<TService, TImplementation>()
                .AddSingleton(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized singleton service of the type specified in <typeparamref name="TService"/> with an implementation
        /// type specified in <typeparamref name="TImplementation"/> using the factory specified in <paramref name="implementationFactory"/>
        /// to the specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</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>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddSingletonLazy<TService, TImplementation>(
            this IServiceCollection services,
            Func<IServiceProvider, TImplementation> implementationFactory)
            where TService : class
            where TImplementation : class, TService
        {
            return services
                .AddSingleton<TService, TImplementation>(implementationFactory)
                .AddSingleton(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized transient service of the type specified in <typeparamref name="TService"/> to the specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddTransientLazy<TService>(this IServiceCollection services)
            where TService : class
        {
            return services
                .AddTransient<TService>()
                .AddTransient(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized transient service of the type specified in <typeparamref name="TService"/> with a factory specified
        /// in <paramref name="implementationFactory"/> to the specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <param name="implementationFactory">The factory that creates the service.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddTransientLazy<TService>(
            this IServiceCollection services,
            Func<IServiceProvider, TService> implementationFactory)
            where TService : class
        {
            return services
                .AddTransient(implementationFactory)
                .AddTransient(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized transient service of the type specified in <typeparamref name="TService"/> with an implementation
        /// type specified in <typeparamref name="TImplementation"/> to the specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</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>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddTransientLazy<TService, TImplementation>(this IServiceCollection services)
            where TService : class
            where TImplementation : class, TService
        {
            return services
                .AddTransient<TService, TImplementation>()
                .AddTransient(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }

        /// <summary>
        /// Adds a lazily initialized transient service of the type specified in <typeparamref name="TService"/> with an implementation
        /// type specified in <typeparamref name="TImplementation"/> using the factory specified in <paramref name="implementationFactory"/>
        /// to the specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</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>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IServiceCollection AddTransientLazy<TService, TImplementation>(
            this IServiceCollection services,
            Func<IServiceProvider, TImplementation> implementationFactory)
            where TService : class
            where TImplementation : class, TService
        {
            return services
                .AddTransient(implementationFactory)
                .AddTransient(x => new Lazy<TService>(() => x.GetRequiredService<TService>()));
        }
    }
}

@RehanSaeed: эти Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΡ Π½Π΅ Ρ€Π°Π±ΠΎΡ‚Π°ΡŽΡ‚ ΠΏΡ€ΠΈ ΠΏΠΎΠΏΡ‹Ρ‚ΠΊΠ΅ Π·Π°Ρ€Π΅Π³ΠΈΡΡ‚Ρ€ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ нСсколько Ρ€Π΅Π°Π»ΠΈΠ·Π°Ρ†ΠΈΠΉ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΈ Ρ‚ΠΎΠ³ΠΎ ΠΆΠ΅ интСрфСйса ΠΈ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ GetServices.

Π‘Ρ‹Π»Π° Π»ΠΈ эта страница ΠΏΠΎΠ»Π΅Π·Π½ΠΎΠΉ?
0 / 5 - 0 Ρ€Π΅ΠΉΡ‚ΠΈΠ½Π³ΠΈ

Π‘ΠΌΠ΅ΠΆΠ½Ρ‹Π΅ вопросы

tuespetre picture tuespetre  Β·  31ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ

ZhiqiangTao picture ZhiqiangTao  Β·  5ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ

tarekgh picture tarekgh  Β·  11ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ

SherifRefaat picture SherifRefaat  Β·  7ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ

divega picture divega  Β·  18ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ