DI рдХреЛ рдЖрд▓рд╕реА рдХрд╛ рд╕рдорд░реНрдерди рдХрд░рдирд╛ рдЪрд╛рд╣рд┐рдП
рд╣рдо рдХрднреА рднреА рддреБрд░рдВрдд рддрддреНрдХрд╛рд▓ рдирд╣реАрдВ рдХрд░рдирд╛ рдЪрд╛рд╣рддреЗ рд╣реИрдВред
рдореБрдЭреЗ рдпрд╣ рд╡рд┐рдЪрд╛рд░ рдкрд╕рдВрдж рд╣реИ, рд▓реЗрдХрд┐рди рд╕рднреА IoC рдХрдВрдЯреЗрдирд░ рдирд╣реАрдВ, рд╣рдо рдЪрд╛рд╣рддреЗ рд╣реИрдВ рдХрд┐ ASP.NET рдЗрд╕рдХрд╛ рд╕рдорд░реНрдерди рдХрд░реЗред рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, рдРрд╕рд╛ рд▓рдЧрддрд╛ рд╣реИ рдХрд┐ рдпрд╣ рд╕реБрд╡рд┐рдзрд╛ рдирд┐рдирдЬреЗрдХреНрдЯ рдХреЗ рд╡рд┐рд╕реНрддрд╛рд░ рдХреЗ рд░реВрдк рдореЗрдВ рд▓рд╛рдЧреВ рдХреА рдЧрдИ рд╣реИ ред
рдпрджрд┐ рдЖрдк рдЕрдкрдиреЗ рдРрдк рдореЗрдВ Lazy<T>
рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдирд╛ рдЪрд╛рд╣рддреЗ рд╣реИрдВ, рддреЛ рдЖрдк Autofac рдпрд╛ Ninject (рдПрдХреНрд╕рдЯреЗрдВрд╢рди рдХреЗ рд╕рд╛рде) рдЬреИрд╕реЗ рдкреВрд░реНрдг рд╡рд┐рд╢реЗрд╖рддрд╛рдУрдВ рд╡рд╛рд▓реЗ IoC рдХрдВрдЯреЗрдирд░ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдирд╛ рдЪреБрди рд╕рдХрддреЗ рд╣реИрдВред
рдЗрд╕ рд╕реБрд╡рд┐рдзрд╛ рдХрд╛ рд╕рдорд░реНрдерди рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рд╕рднреА IoC рдХрдВрдЯреЗрдирд░реЛрдВ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рдХреЗ рдмрд┐рдирд╛, рдмрд╣реБрдд рдХрдо рдкреНрд░рдпрд╛рд╕ рдХреЗ рд╕рд╛рде рдЖрд▓рд╕реА рд╕рдорд░реНрдерди рдЬреЛрдбрд╝рдиреЗ рдХреЗ рд▓рд┐рдП 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 рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рдХрд╛ рдкреНрд░рдпрд╛рд╕ рдХрд░рддреЗ рд╕рдордп рдпреЗ рдПрдХреНрд╕рдЯреЗрдВрд╢рди рдХрд╛рдо рдирд╣реАрдВ рдХрд░рддреЗ рд╣реИрдВ
рд╕рдмрд╕реЗ рдЙрдкрдпреЛрдЧреА рдЯрд┐рдкреНрдкрдгреА
рдЗрд╕ рд╕реБрд╡рд┐рдзрд╛ рдХрд╛ рд╕рдорд░реНрдерди рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рд╕рднреА IoC рдХрдВрдЯреЗрдирд░реЛрдВ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рдХреЗ рдмрд┐рдирд╛, рдмрд╣реБрдд рдХрдо рдкреНрд░рдпрд╛рд╕ рдХреЗ рд╕рд╛рде рдЖрд▓рд╕реА рд╕рдорд░реНрдерди рдЬреЛрдбрд╝рдиреЗ рдХреЗ рд▓рд┐рдП
IServiceCollection
рд╕реЗ рдЕрдзрд┐рдХ рд╡рд┐рд╕реНрддрд╛рд░ рд╡рд┐рдзрд┐рдпреЛрдВ рдХреЛ рдЫрд┐рдбрд╝рдХрдирд╛ рд╕рдВрднрд╡ рд╣реИред рд╡рд┐рдЪрд╛рд░?