DEV Community

Cover image for Lazy Dependency Injection for Microsoft DI ServiceProvider
Aleksei Ananev
Aleksei Ananev

Posted on

Lazy Dependency Injection for Microsoft DI ServiceProvider

Dependency Injection is a powerful tool and standard for achieving quality code. However, this can adversely affect performance and memory consumption, as instances may be created that are not used for the current request.

The previous post detailed the problem and one solution that allows gracefully solving it without polluting your code. This solution is to use LazyProxy for IoC containers. The main idea is to substitute an object of the real type with a proxy. This proxy is a runtime generated type that implemented a specific interface T and called all members of this interface through Lazy<T>. Thus, the behavior changes as if Lazy<T> was injected instead of the T interface. Instances are created only on demand, but at the same time, the code remains clean.

Today I am happy to announce that LazyProxy became available for one more container. LazyProxy.ServiceProvider library allows lazy dependency injection for Microsoft's DI ServiceProvider.

Usage of this library is similar to already existing libraries LazyProxy.Unity and LazyProxy.Autofac.

Here is an example of how to use this library. Consider the following interface and implementation:

public interface IFoo
{
    void Bar();
}

public class Foo: IFoo
{
    public Foo() => Console.WriteLine("Ctor");
    public void Bar() => Console.WriteLine("Bar");
}
Enter fullscreen mode Exit fullscreen mode

A lazy registration for this service can be added like this:

// Creating a service collection
var services = new ServiceCollection();

// Adding a lazy registration
services.AddLazyScoped<IMyService, MyService>();

// Building the service provider
var serviceProvider = services.BuildServiceProvider()
Enter fullscreen mode Exit fullscreen mode

Consider the following usage of the registered service:

Console.WriteLine("Resolving the service...");
var foo = serviceProvider.GetService<IFoo>();

Console.WriteLine("Executing the 'Bar' method...");
foo.Bar();
Enter fullscreen mode Exit fullscreen mode

The output:

Resolving the service...
Executing the 'Bar' method...
Ctor
Bar
Enter fullscreen mode Exit fullscreen mode

As you can see from the output, the constructor is not called until the first call to method Bar is made.

The initiative and brilliant initial implementation of the LazyProxy.ServiceProvider library belong entirely to Carlos Mendible, for which I want to express gratitude and appreciation on behalf of the entire open-source community. Besides, Carlos has generously permitted to move the new repository alongside all other LazyProxy's repositories. That was a big step forward in the lazy dependency injection ideas.

GitHub Repositories

LazyProxy
LazyProxy.Autofac
LazyProxy.Unity
LazyProxy.ServiceProvider

Top comments (1)

Collapse
 
cmendibl3 profile image
Carlos Mendible

Wow!!! thanks for the mention! It was nice to collaborate with you!