DI Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°ΡΡ Lazy
ΠΡ Π½Π΅ Ρ
ΠΎΡΠΈΠΌ ΠΊΠΎΠ³Π΄Π°-Π½ΠΈΠ±ΡΠ΄Ρ ΡΡΠ°Π·Ρ ΡΠΎΠ·Π΄Π°Π²Π°ΡΡ ΡΠΊΠ·Π΅ΠΌΠΏΠ»ΡΡ.
ΠΠ½Π΅ Π½ΡΠ°Π²ΠΈΡΡΡ ΡΡΠ° ΠΈΠ΄Π΅Ρ, Π½ΠΎ Π½Π΅ Π²ΡΠ΅ ΠΊΠΎΠ½ΡΠ΅ΠΉΠ½Π΅ΡΡ 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.
Π‘Π°ΠΌΡΠΉ ΠΏΠΎΠ»Π΅Π·Π½ΡΠΉ ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΉ
ΠΠΎΠΆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΌΠ΅ΡΠΎΠ΄ΠΎΠ² ΡΠ°ΡΡΠΈΡΠ΅Π½ΠΈΡ ΠΊ
IServiceCollection
ΡΡΠΎΠ±Ρ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΊΡ Lazy Ρ ΠΎΡΠ΅Π½Ρ Π½Π΅Π±ΠΎΠ»ΡΡΠΈΠΌΠΈ ΡΡΠΈΠ»ΠΈΡΠΌΠΈ, Π½Π΅ ΡΡΠ΅Π±ΡΡ, ΡΡΠΎΠ±Ρ Π²ΡΠ΅ ΠΊΠΎΠ½ΡΠ΅ΠΉΠ½Π΅ΡΡ IoC ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π»ΠΈ ΡΡΡ ΡΡΠ½ΠΊΡΠΈΡ. ΠΡΡΠ»ΠΈ?