A LazyProxy can be used for IoC containers to improve performance by changing the resolve behavior.
More info can be found in the article about Lazy Dependency Injection for .NET.
The library provides in NuGet.
Install-Package LazyProxy.Autofac
Consider the following service:
public interface IMyService
{
void Foo();
}
public class MyService : IMyService
{
public MyService() => Console.WriteLine("Ctor");
public void Foo() => Console.WriteLine("Foo");
}
A lazy registration for this service can be added like this:
// Creating a container builder
var containerBuilder = new ContainerBuilder();
// Adding a lazy registration
containerBuilder.RegisterLazy<IMyService, MyService>();
// Building a container
using var container = containerBuilder.Build();
Console.WriteLine("Resolving the service...");
var service = container.Resolve<IMyService>();
Console.WriteLine("Executing the 'Foo' method...");
service.Foo();
The output for this example:
Resolving the service...
Executing the 'Foo' method...
Ctor
Foo
This project is licensed under the Apache License, Version 2.0. - see the LICENSE file for details.