diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 4bacb366..6d6ba6d6 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -8,6 +8,7 @@ using AsbCloudInfrastructure.Services.WellOperationService; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using System; namespace AsbCloudInfrastructure { @@ -66,5 +67,17 @@ namespace AsbCloudInfrastructure return services; } + + public static IServiceCollection AddTransientLazy(this IServiceCollection services) + where TService : class + where TImplementation : class, TService + => services.AddTransient() + .AddTransient(provider => new Lazy(provider.GetService)); + + public static IServiceCollection AddTransientLazy(this IServiceCollection services, Func implementationFactory) + where TService : class + where TImplementation : class, TService + => services.AddTransient(implementationFactory) + .AddTransient(provider => new Lazy(() => implementationFactory(provider))); } }