You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+67-22
Original file line number
Diff line number
Diff line change
@@ -1,37 +1,84 @@
1
-
# Dynamic Lazy Proxy
1
+
# LazyProxy
2
2
3
-
A dynamic lazy proxy is a class built in real time, that implemenets some interface `T`, takes to the constructor an argument `Lazy<T>`and routes all invocations to the corresponding method or property of this argument.
3
+
`LazyProxy` is a lightweight library allowing to build a lazy proxy type for some interface `T` at runtime. The proxy type implements this interface and is initialized by the `Lazy<T>`argument. All method and property invocations route to the corresponding members of the lazy's `Value`.
4
4
5
-
The real instance wrapped by `Lazy<T>`is created only after the first invocation of method or property. It allows to distribute the loading from the class creation to the method or property invocation.
5
+
For illustration, assume there is the following interface:
6
6
7
-
```C#
7
+
```CSharp
8
8
publicinterfaceIMyService
9
9
{
10
-
voidFoo();
10
+
voidFoo();
11
+
}
12
+
```
13
+
14
+
Then the generated lazy proxy type looks like this:
15
+
16
+
```CSharp
17
+
// In reality, the implementation is a little more complicated,
18
+
// but the details are omitted for ease of understanding.
Console.WriteLine("The real instance creation...");
22
-
returnnewMyService();
62
+
Console.WriteLine("Creating an instance of the real service...");
63
+
returnnewMyService();
23
64
});
24
65
25
-
Console.WriteLine("Foo execution...");
26
-
proxy.Foo();
66
+
Console.WriteLine("Executing the 'Foo' method...");
67
+
lazyProxy.Foo();
68
+
```
69
+
70
+
The output for this example:
27
71
28
-
// Foo execution...
29
-
// The real instance creation...
30
-
// Hello from ctor
31
-
// Hello from Foo
32
72
```
73
+
Executing the 'Foo' method...
74
+
Creating an instance of the real service...
75
+
Ctor
76
+
Foo
77
+
```
78
+
79
+
## Features
33
80
34
-
The following is supported:
81
+
Currently, `LazyProxy` supports the following:
35
82
- Void/Result methods;
36
83
- Async methods;
37
84
- Generic methods;
@@ -46,13 +93,11 @@ The following is supported:
46
93
**Not supported yet:**
47
94
- Events
48
95
49
-
## Lazy injection for IoC containers
50
-
51
-
A dynamic lazy proxy can be used for IoC containers to change the resolving behaviour.
96
+
## Lazy Dependency Injection
52
97
53
-
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.
98
+
Lazy proxies can be used for IoC containers to improve performance by changing resolution behavior.
54
99
55
-
Also dynamic lazy proxy allows injection of circular dependencies.
100
+
More info can be found in the article about [Lazy Dependency Injection for .NET](https://dev.to/hypercodeplace/lazy-dependency-injection-37en).
56
101
57
102
[Lazy injection for Unity container](https://github.com/servicetitan/lazy-proxy-unity)
0 commit comments