Dependencyinjection: 지원이 ν•„μš”ν•©λ‹ˆλ‹€<t/>

에 λ§Œλ“  2014λ…„ 08μ›” 19일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: aspnet/DependencyInjection

DIλŠ” Lazyλ₯Ό 지원해야 ν•©λ‹ˆλ‹€..
μš°λ¦¬λŠ” μ–Έμ  κ°€ μ¦‰μ‹œ μΈμŠ€ν„΄μŠ€ν™”ν•˜κ³  싢지 μ•ŠμŠ΅λ‹ˆλ‹€.

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

λͺ¨λ“  IoC μ»¨ν…Œμ΄λ„ˆκ°€ 이 κΈ°λŠ₯을 μ§€μ›ν•˜μ§€ μ•Šμ•„λ„ μ•½κ°„μ˜ λ…Έλ ₯으둜 Lazy 지원을 μΆ”κ°€ν•˜κΈ° μœ„ν•΄ IServiceCollection μœ„μ— λͺ‡ 가지 ν™•μž₯ 방법을 뿌릴 수 μžˆμŠ΅λ‹ˆλ‹€. 생각?

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 λŒ“κΈ€

이 아이디어가 λ§ˆμŒμ— λ“€μ§€λ§Œ ASP.NETμ—μ„œ μ‹€ν–‰λ˜κΈ°λ₯Ό μ›ν•˜λŠ” λͺ¨λ“  IoC μ»¨ν…Œμ΄λ„ˆκ°€ 이λ₯Ό μ§€μ›ν•˜μ§€λŠ” μ•ŠμŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄ 이 κΈ°λŠ₯은 Ninject용 ν™•μž₯으둜 κ΅¬ν˜„λœ 것 κ°™μŠ΅λ‹ˆλ‹€.

μ•±μ—μ„œ Lazy<T> λ₯Ό μ‚¬μš©ν•˜λ €λ©΄ Autofac λ˜λŠ” Ninject(ν™•μž₯ 포함)와 같은 μ™„μ „ν•œ κΈ°λŠ₯을 κ°–μΆ˜ IoC μ»¨ν…Œμ΄λ„ˆλ₯Ό μ‚¬μš©ν•˜λ„λ‘ 선택할 수 μžˆμŠ΅λ‹ˆλ‹€.

λͺ¨λ“  IoC μ»¨ν…Œμ΄λ„ˆκ°€ 이 κΈ°λŠ₯을 μ§€μ›ν•˜μ§€ μ•Šμ•„λ„ μ•½κ°„μ˜ λ…Έλ ₯으둜 Lazy 지원을 μΆ”κ°€ν•˜κΈ° μœ„ν•΄ IServiceCollection μœ„μ— λͺ‡ 가지 ν™•μž₯ 방법을 뿌릴 수 μžˆμŠ΅λ‹ˆλ‹€. 생각?

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 λ“±κΈ‰