From 5c63c086ad6a0018c33f4b520a6e922efac451fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Wed, 1 Dec 2021 11:49:59 +0500 Subject: [PATCH] CS2-127 Add lazy service registration. --- AsbCloudInfrastructure/DependencyInjection.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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))); } }