DotNetBlocks.Extensions.DependencyInjection
Adds functionality to the Microsoft.Extensions namespace to solve Lazy
Getting started
NuGet : DotNetBlocks.Extensions.DependencyInjection
Licensing and other information
Functionality
DependencyInjection
To improve performance, design patterns use the injection of Lazy
Problems Solved
Lack of support for lazy types in Microsoft.Extensions.DependencyInjection
#Examples
Dependency Injection Problem Solved.
Microsoft DI is the simplest DI system available and does not support inferring Lazy
If you use MS DI, you have to registee the Lazy
We need to add the functionality to create any Lazy
Alternatives
A better solution is to use AutoFac or other open source DI systems and leverage the more advanced functionality.
The Solution
References
Microsoft DI Default Service Container Replacement
Implementation notes
A first attempt was to use a lighter facade factory pattern using implicit type conventions, but issues with Di type conversion testing on implicit classes prevented its use.
The final lazy implementation had to exactly match the lazy
Solution inspiration is this discussion aboard Solution ideas discussion - for credit
details
This library implements two solutions internally. One uses a Lazy Implementation class that inherits from Lazy but with a constructor with DI support. This is a simple but effective solution to the problem
and is the soution implementation when you "add Lazy Support" to the service collection. It registers this LazyService type for use in all lazy
The second implementation is a "true lazy" implementation using a delegate constructor to create an true Lazy class with a dynamic costructor. This is the solution used when you use a Lazy registration for a type or the AsLazy registration extension method. The "true" implementation is modeled on the standard microsoft libraries, using copied and modified versions of the microsoft DI service registration and service descriptor static methods.
The internal pattern is to create ServiceDescriptors using the "describe" pattern and add those to the service collection. The describe method creates the appropriate factories and lifetimes supporting the requeted lazy usage registration type.
Factory methods
The factory methods use methodInfo to close the types and invoke the factory method builder, but its important to note this does not have a performance impact, because the builder method is only called during registration to create an appropriate delegate that is passed to the dependency registration. This technique is faster than building the lambdas using code and more maintainable.