Lazy injection for Microsoft.Extensions.DependencyInjection using the Proxy pattern.
Decorates registered services with lazy proxies that instantiate the original service only after the first method or property call.
Intended to prevent the creation of unused injected dependencies.
using LazyDependencyInjection;
var services = new ServiceCollection()
.AddTransient<IExampleService1, ExampleService1>()
.AddTransient<IExampleService2, ExampleService2>()
.AddTransient<IExampleService3, ExampleService3>()
.AddTransient<ServiceWithManyUnusedDeps>()
// DEFAULT: Adds for any service that has dependencies
// and injections into other services with multiple methods and dependencies
.AddLazyProxy()
// OR: Default analog with additional assembly condition for services
.AddLazyProxy(Assembly.GetExecutingAssembly())
// OR: Adds only for specified services
.AddLazyProxy(typeof(IExampleService1), typeof(IExampleService2))
// OR: Adds with your custom filter
.AddLazyProxy(x => !x.Descriptor.IsKeyedService && x.Descriptor.HasDependenciesCountGreaterThan(0))
// OR: Adds for any service that has dependencies and injections into specific services
.AddLazyProxy(ServiceFilters.HasDependencies()
.And(ServiceFilters.IsInjectedTo(typeof(ServiceWithManyUnusedDeps))))
.BuildServiceProvider();