Skip to content

Commit faf3b97

Browse files
Merge pull request #10 from servicetitan/readme-imrovements
Readme improvements
2 parents 59b5cbd + b97a637 commit faf3b97

File tree

4 files changed

+59
-24
lines changed

4 files changed

+59
-24
lines changed

LazyProxy.Unity.Sample/LazyProxy.Unity.Sample.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
44
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
<LangVersion>8</LangVersion>
56
</PropertyGroup>
67
<ItemGroup>
78
<PackageReference Include="Unity.Container" Version="5.10.2" />

LazyProxy.Unity.Sample/Program.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ public static void Main(string[] args)
1616

1717
private static void UnityExtensionExample1()
1818
{
19-
var container = new UnityContainer()
20-
.RegisterLazy<IMyService, MyService>();
19+
// Creating a container
20+
using var container = new UnityContainer();
21+
22+
// Adding a lazy registration
23+
container.RegisterLazy<IMyService, MyService>();
2124

22-
Console.WriteLine("Resolving service...");
25+
Console.WriteLine("Resolving the service...");
2326
var service = container.Resolve<IMyService>();
2427

25-
Console.WriteLine("Foo execution...");
28+
Console.WriteLine("Executing the 'Foo' method...");
2629
service.Foo();
2730
}
2831

@@ -56,4 +59,4 @@ private static void UnityExtensionExample2()
5659
}
5760
}
5861
}
59-
}
62+
}

LazyProxy.Unity.Sample/Services.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public interface IMyService
1010

1111
public class MyService : IMyService
1212
{
13-
public MyService() => Console.WriteLine("Hello from ctor");
14-
public void Foo() => Console.WriteLine("Hello from Foo");
13+
public MyService() => Console.WriteLine("Ctor");
14+
public void Foo() => Console.WriteLine("Foo");
1515
}
1616

1717
public abstract class Warrior
@@ -111,4 +111,4 @@ public Weapon CreateShuriken()
111111
return new Weapon(damage);
112112
}
113113
}
114-
}
114+
}

README.md

+47-16
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,67 @@
1-
# Lazy injection for Unity container
1+
# Lazy Dependency Injection for Unity Container
22

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

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

7-
Also dynamic lazy proxy allows injection of circular dependencies.
7+
## Get Packages
88

9-
```C#
10-
var container = new UnityContainer().RegisterLazy<IMyService, MyService>();
9+
The library provides in NuGet.
1110

12-
Console.WriteLine("Resolving service...");
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
35+
// Creating a container
36+
using var container = new UnityContainer();
37+
38+
// Adding a lazy registration
39+
container.RegisterLazy<IMyService, MyService>();
40+
41+
Console.WriteLine("Resolving the service...");
1342
var service = container.Resolve<IMyService>();
1443

15-
Console.WriteLine("Foo execution...");
44+
Console.WriteLine("Executing the 'Foo' method...");
1645
service.Foo();
46+
```
1747

18-
// Resolving service...
19-
// Foo execution...
20-
// Hello from ctor
21-
// Hello from Foo
48+
The output for this example:
2249

2350
```
51+
Resolving the service...
52+
Executing the 'Foo' method...
53+
Ctor
54+
Foo
55+
```
56+
57+
## Features
2458

25-
The following is supported:
59+
Currently, `LazyProxy.Unity` supports the following:
2660
- Registration of types by interfaces;
2761
- Passing lifetime managers;
2862
- Passing injection members;
2963
- Resolving by child containers.
3064

31-
**Not supported yet:**
32-
- Registration of instances.
33-
3465
## Performance
3566

3667
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

Comments
 (0)