Skip to content

Commit ee77c78

Browse files
Merge pull request #11 from servicetitan/readme-improvements
Readme improvements
2 parents fed3fa2 + f6fbbef commit ee77c78

File tree

3 files changed

+75
-30
lines changed

3 files changed

+75
-30
lines changed

LazyProxy.Sample/Program.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ internal class Program
66
{
77
public static void Main(string[] args)
88
{
9-
var proxy = LazyProxyBuilder.CreateInstance<IMyService>(() =>
9+
var lazyProxy = LazyProxyBuilder.CreateInstance<IMyService>(() =>
1010
{
11-
Console.WriteLine("The real instance creation...");
11+
Console.WriteLine("Creating an instance of the real service...");
1212
return new MyService();
1313
});
1414

15-
Console.WriteLine("Foo execution...");
16-
proxy.Foo();
15+
Console.WriteLine("Executing the 'Foo' method...");
16+
lazyProxy.Foo();
1717
}
1818
}
19-
}
19+
}

LazyProxy.Sample/Services.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface IMyService
99

1010
public class MyService : IMyService
1111
{
12-
public MyService() => Console.WriteLine("Hello from ctor");
13-
public void Foo() => Console.WriteLine("Hello from Foo");
12+
public MyService() => Console.WriteLine("Ctor");
13+
public void Foo() => Console.WriteLine("Foo");
1414
}
15-
}
15+
}

README.md

+67-22
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,84 @@
1-
# Dynamic Lazy Proxy
1+
# LazyProxy
22

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`.
44

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:
66

7-
```C#
7+
```CSharp
88
public interface IMyService
99
{
10-
void Foo();
10+
void Foo();
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.
19+
public class LazyProxyImpl_IMyService : IMyService
20+
{
21+
private Lazy<IMyService> _service;
22+
23+
public LazyProxyImpl_IMyService(Lazy<IMyService> service)
24+
{
25+
_service = service;
26+
}
27+
28+
public void Foo() => _service.Value.Foo();
29+
}
30+
```
31+
32+
## Get Packages
33+
34+
The library provides in NuGet.
35+
36+
```
37+
Install-Package LazyProxy
38+
```
39+
40+
## Get Started
41+
42+
Consider the following service:
43+
44+
```CSharp
45+
public interface IMyService
46+
{
47+
void Foo();
1148
}
1249

1350
public class MyService : IMyService
1451
{
15-
public MyService() => Console.WriteLine("Hello from ctor");
16-
public void Foo() => Console.WriteLine("Hello from Foo");
52+
public MyService() => Console.WriteLine("Ctor");
53+
public void Foo() => Console.WriteLine("Foo");
1754
}
55+
```
1856

19-
var proxy = LazyProxyBuilder.CreateLazyProxyInstance<IMyService>(() =>
57+
A lazy proxy instance for this service can be created like this:
58+
59+
```CSharp
60+
var lazyProxy = LazyProxyBuilder.CreateInstance<IMyService>(() =>
2061
{
21-
Console.WriteLine("The real instance creation...");
22-
return new MyService();
62+
Console.WriteLine("Creating an instance of the real service...");
63+
return new MyService();
2364
});
2465

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:
2771

28-
// Foo execution...
29-
// The real instance creation...
30-
// Hello from ctor
31-
// Hello from Foo
3272
```
73+
Executing the 'Foo' method...
74+
Creating an instance of the real service...
75+
Ctor
76+
Foo
77+
```
78+
79+
## Features
3380

34-
The following is supported:
81+
Currently, `LazyProxy` supports the following:
3582
- Void/Result methods;
3683
- Async methods;
3784
- Generic methods;
@@ -46,13 +93,11 @@ The following is supported:
4693
**Not supported yet:**
4794
- Events
4895

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
5297

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.
5499

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).
56101

57102
[Lazy injection for Unity container](https://github.com/servicetitan/lazy-proxy-unity)
58103

0 commit comments

Comments
 (0)