|
1 |
| -# Lazy injection for Autofac container |
| 1 | +# Lazy Dependency Injection for Autofac 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.Autofac |
| 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 |
| 35 | +// Creating a container builder |
10 | 36 | var containerBuilder = new ContainerBuilder();
|
11 |
| -containerBuilder.RegisterLazy<IFoo, Foo>(); |
12 |
| -var container = containerBuilder.Build(); |
13 | 37 |
|
14 |
| -Console.WriteLine("Resolving service..."); |
15 |
| -var foo = container.Resolve<IFoo>(); |
| 38 | +// Adding a lazy registration |
| 39 | +containerBuilder.RegisterLazy<IMyService, MyService>(); |
16 | 40 |
|
17 |
| -Console.WriteLine("Bar execution..."); |
18 |
| -foo.Bar(); |
| 41 | +// Building a container |
| 42 | +using var container = containerBuilder.Build(); |
19 | 43 |
|
20 |
| -// Resolving service... |
21 |
| -// Bar execution... |
22 |
| -// Hello from ctor |
23 |
| -// Hello from Bar |
| 44 | +Console.WriteLine("Resolving the service..."); |
| 45 | +var service = container.Resolve<IMyService>(); |
24 | 46 |
|
| 47 | +Console.WriteLine("Executing the 'Foo' method..."); |
| 48 | +service.Foo(); |
| 49 | +``` |
| 50 | + |
| 51 | +The output for this example: |
| 52 | + |
| 53 | +``` |
| 54 | +Resolving the service... |
| 55 | +Executing the 'Foo' method... |
| 56 | +Ctor |
| 57 | +Foo |
25 | 58 | ```
|
26 | 59 |
|
27 | 60 | ## License
|
28 | 61 |
|
29 |
| -This project is licensed under the Apache License, Version 2.0. - see the [LICENSE](https://github.com/servicetitan/lazy-proxy-unity/blob/master/LICENSE) file for details. |
| 62 | +This project is licensed under the Apache License, Version 2.0. - see the [LICENSE](https://github.com/servicetitan/lazy-proxy-Autofac/blob/master/LICENSE) file for details. |
0 commit comments