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:
publicinterfaceIMyService{voidFoo();}publicclassMyService:IMyService{publicMyService()=>Console.WriteLine("Ctor");publicvoidFoo()=>Console.WriteLine("Foo");}A lazy registration for this service can be added like this:
// Creating a container buildervarcontainerBuilder=newContainerBuilder();// Adding a lazy registrationcontainerBuilder.RegisterLazy<IMyService,MyService>();// Building a containerusingvarcontainer=containerBuilder.Build();Console.WriteLine("Resolving the service...");varservice=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.