Skip to content

Commit a4efabb

Browse files
Improve README.
1 parent 55f52ef commit a4efabb

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

README.md

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,62 @@
1-
# Lazy injection for Autofac container
1+
# Lazy Dependency Injection for Autofac 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#
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
1036
var containerBuilder = new ContainerBuilder();
11-
containerBuilder.RegisterLazy<IFoo, Foo>();
12-
var container = containerBuilder.Build();
1337

14-
Console.WriteLine("Resolving service...");
15-
var foo = container.Resolve<IFoo>();
38+
// Adding a lazy registration
39+
containerBuilder.RegisterLazy<IMyService, MyService>();
1640

17-
Console.WriteLine("Bar execution...");
18-
foo.Bar();
41+
// Building a container
42+
using var container = containerBuilder.Build();
1943

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>();
2446

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
2558
```
2659

2760
## License
2861

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

Comments
 (0)