Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mustaddon/lazydependencyinjection

Lazy injection for Microsoft.Extensions.DependencyInjection. 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.
https://github.com/mustaddon/lazydependencyinjection

dependency-injection dispatch-proxy lazy-injection lazy-loading lazy-proxy lazy-service proxy-pattern service-collection service-provider virtual-proxy

Last synced: 28 days ago
JSON representation

Lazy injection for Microsoft.Extensions.DependencyInjection. 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.

Awesome Lists containing this project

README

        

# LazyDependencyInjection [![NuGet version](https://badge.fury.io/nu/LazyDependencyInjection.svg?105)](http://badge.fury.io/nu/LazyDependencyInjection)
Lazy injection for Microsoft.Extensions.DependencyInjection using the [Proxy pattern](https://en.wikipedia.org/wiki/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.

### Example
```C#
using LazyDependencyInjection;

var services = new ServiceCollection()
.AddTransient()
.AddTransient()
.AddTransient()
.AddTransient()

// 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();
```

[Program.cs](https://github.com/mustaddon/LazyDependencyInjection/blob/main/Example/Program.cs)

### Concept
[![](https://raw.githubusercontent.com/mustaddon/LazyDependencyInjection/master/dgrm.png)](https://app.dgrm.net/?u=https://raw.githubusercontent.com/mustaddon/LazyDependencyInjection/master/dgrm.png)