|
1 |
| -# Lazy injection for Unity container |
| 1 | +# Lazy Dependency Injection for Unity Container |
2 | 2 |
|
3 |
| -A [LazyProxy](https://github.com/servicetitan/lazy-proxy) can be used for IoC containers to change the resolving behaviour. |
| 3 | +A [LazyProxy](https://github.com/servicetitan/lazy-proxy) can be used for IoC containers to improve performance by changing the resolve behavior. |
4 | 4 |
|
5 |
| -Dependencies registered as lazy are created as dynamic proxy objects built in real time, but the real classes are resolved only after the first execution of proxy method or property. |
| 5 | +More info can be found in the article about [Lazy Dependency Injection for .NET](https://dev.to/hypercodeplace/lazy-dependency-injection-37en). |
6 | 6 |
|
7 |
| -Also dynamic lazy proxy allows injection of circular dependencies. |
| 7 | +## Get Packages |
8 | 8 |
|
9 |
| -```C# |
| 9 | +The library provides in NuGet. |
| 10 | + |
| 11 | +``` |
| 12 | +Install-Package LazyProxy.Unity |
| 13 | +``` |
| 14 | + |
| 15 | +## Get Started |
| 16 | + |
| 17 | +Consider the following service: |
| 18 | + |
| 19 | +```CSharp |
| 20 | +public interface IMyService |
| 21 | +{ |
| 22 | + void Foo(); |
| 23 | +} |
| 24 | + |
| 25 | +public class MyService : IMyService |
| 26 | +{ |
| 27 | + public MyService() => Console.WriteLine("Ctor"); |
| 28 | + public void Foo() => Console.WriteLine("Foo"); |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +A lazy registration for this service can be added like this: |
| 33 | + |
| 34 | +```CSharp |
10 | 35 | var container = new UnityContainer().RegisterLazy<IMyService, MyService>();
|
11 | 36 |
|
12 |
| -Console.WriteLine("Resolving service..."); |
| 37 | +Console.WriteLine("Resolving the service..."); |
13 | 38 | var service = container.Resolve<IMyService>();
|
14 | 39 |
|
15 |
| -Console.WriteLine("Foo execution..."); |
| 40 | +Console.WriteLine("Executing the 'Foo' method..."); |
16 | 41 | service.Foo();
|
| 42 | +``` |
17 | 43 |
|
18 |
| -// Resolving service... |
19 |
| -// Foo execution... |
20 |
| -// Hello from ctor |
21 |
| -// Hello from Foo |
| 44 | +The output for this example: |
22 | 45 |
|
23 | 46 | ```
|
| 47 | +Resolving the service... |
| 48 | +Executing the 'Foo' method... |
| 49 | +Ctor |
| 50 | +Foo |
| 51 | +``` |
| 52 | + |
| 53 | +## Features |
24 | 54 |
|
25 |
| -The following is supported: |
| 55 | +Currently, `LazyProxy.Unity` supports the following: |
26 | 56 | - Registration of types by interfaces;
|
27 | 57 | - Passing lifetime managers;
|
28 | 58 | - Passing injection members;
|
29 | 59 | - Resolving by child containers.
|
30 | 60 |
|
31 |
| -**Not supported yet:** |
32 |
| -- Registration of instances. |
33 |
| - |
34 | 61 | ## Performance
|
35 | 62 |
|
36 | 63 | Here is a result of the [Benchmark test](https://github.com/servicetitan/lazy-proxy-unity/blob/master/LazyProxy.Unity.Benchmarks/UnityExtensionBenchmark.cs)
|
|
0 commit comments