Skip to content

Commit 674be57

Browse files
Update README.md
1 parent 3380b8f commit 674be57

File tree

17 files changed

+237
-29
lines changed

17 files changed

+237
-29
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ dotnet run
262262
- [Complex generic root arguments](readme/complex-generic-root-arguments.md)
263263
- [Generic builder](readme/generic-builder.md)
264264
- [Generic builders](readme/generic-builders.md)
265+
- [Generic roots](readme/generic-roots.md)
265266
### Attributes
266267
- [Constructor ordinal attribute](readme/constructor-ordinal-attribute.md)
267268
- [Dependency attribute](readme/dependency-attribute.md)

readme/WebAPI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal partial class Composition: ServiceProviderFactory<Composition>
2020
// Use the DI setup from the base class
2121
.DependsOn(Base)
2222
.Bind().As(Singleton).To<WeatherForecastService>()
23-
// Provides controllers as roots
23+
// Registers controllers as roots
2424
.Roots<ControllerBase>();
2525
}
2626
```

readme/WebAPIPageTemplate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal partial class Composition: ServiceProviderFactory<Composition>
2020
// Use the DI setup from the base class
2121
.DependsOn(Base)
2222
.Bind().As(Singleton).To<WeatherForecastService>()
23-
// Provides controllers as roots
23+
// Registers controllers as roots
2424
.Roots<ControllerBase>();
2525
}
2626
```

readme/WebApp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal partial class Composition: ServiceProviderFactory<Composition>
2020
// Use the DI setup from the base class
2121
.DependsOn(Base)
2222
.Bind().As(Singleton).To<WeatherForecastService>()
23-
// Provides controllers as roots
23+
// Registers controllers as roots
2424
.Roots<Controller>();
2525
}
2626
```

readme/WebAppPageTemplate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal partial class Composition: ServiceProviderFactory<Composition>
2020
// Use the DI setup from the base class
2121
.DependsOn(Base)
2222
.Bind().As(Singleton).To<WeatherForecastService>()
23-
// Provides controllers as roots
23+
// Registers controllers as roots
2424
.Roots<Controller>();
2525
}
2626
```

readme/async-disposable-scope.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ partial class Composition: IDisposable, IAsyncDisposable
172172
() =>
173173
{
174174
Composition transientComposition3 = this;
175-
Session localValue98 = new Session(transientComposition3);
176-
return localValue98;
175+
Session localValue99 = new Session(transientComposition3);
176+
return localValue99;
177177
});
178178
return new Program(perBlockFunc1);
179179
}

readme/auto-scoped.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ partial class Composition
129129
{
130130
Composition transientComposition3 = this;
131131
IService transientIService2;
132-
Composition localBaseComposition100 = transientComposition3;
132+
Composition localBaseComposition101 = transientComposition3;
133133
// Creates a session
134-
var localSession101 = new Composition(localBaseComposition100);
134+
var localSession102 = new Composition(localBaseComposition101);
135135
// Provides a root
136-
transientIService2 = localSession101.SessionRoot;
137-
IService localValue99 = transientIService2;
138-
return localValue99;
136+
transientIService2 = localSession102.SessionRoot;
137+
IService localValue100 = transientIService2;
138+
return localValue100;
139139
});
140140
return new Program(perBlockFunc1);
141141
}

readme/basic-unity-use-case.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ partial class Composition
130130
}
131131

132132
Clock transientClock0;
133-
Clock localBuildingInstance106 = buildingInstance;
134-
localBuildingInstance106.ClockService = _root._singletonClockService43;
135-
transientClock0 = localBuildingInstance106;
133+
Clock localBuildingInstance107 = buildingInstance;
134+
localBuildingInstance107.ClockService = _root._singletonClockService43;
135+
transientClock0 = localBuildingInstance107;
136136
return transientClock0;
137137
}
138138
}

readme/generic-roots.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#### Generic roots
2+
3+
[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](../tests/Pure.DI.UsageTests/Generics/GenericsRootsScenario.cs)
4+
5+
6+
```c#
7+
using Pure.DI;
8+
9+
DI.Setup(nameof(Composition))
10+
// This hint indicates to not generate methods such as Resolve
11+
.Hint(Hint.Resolve, "Off")
12+
.Bind().To<Dependency<TT>>()
13+
.Bind().To<Service<TT>>()
14+
// Creates OtherService manually,
15+
// just for the sake of example
16+
.Bind<OtherService<TT>>().To(ctx =>
17+
{
18+
ctx.Inject(out IDependency<TT> dependency);
19+
return new OtherService<TT>(dependency);
20+
})
21+
22+
// Specifies to define composition roots for all types inherited from IService<TT>
23+
// available at compile time at the point where the method is called
24+
.Roots<IService<TT>>("GetMy{type}");
25+
26+
var composition = new Composition();
27+
28+
// service = new Service<int>(new Dependency<int>());
29+
var service = composition.GetMyService_T<int>();
30+
31+
// someOtherService = new OtherService<int>(new Dependency<int>());
32+
var someOtherService = composition.GetMyOtherService_T<string>();
33+
34+
interface IDependency<T>;
35+
36+
class Dependency<T> : IDependency<T>;
37+
38+
interface IService<T>;
39+
40+
class Service<T>(IDependency<T> dependency) : IService<T>;
41+
42+
class OtherService<T>(IDependency<T> dependency) : IService<T>;
43+
```
44+
45+
<details>
46+
<summary>Running this code sample locally</summary>
47+
48+
- Make sure you have the [.NET SDK 9.0](https://dotnet.microsoft.com/en-us/download/dotnet/9.0) or later is installed
49+
```bash
50+
dotnet --list-sdk
51+
```
52+
- Create a net9.0 (or later) console application
53+
```bash
54+
dotnet new console -n Sample
55+
```
56+
- Add reference to NuGet package
57+
- [Pure.DI](https://www.nuget.org/packages/Pure.DI)
58+
```bash
59+
dotnet add package Pure.DI
60+
```
61+
- Copy the example code into the _Program.cs_ file
62+
63+
You are ready to run the example 🚀
64+
```bash
65+
dotnet run
66+
```
67+
68+
</details>
69+
70+
The following partial class will be generated:
71+
72+
```c#
73+
partial class Composition
74+
{
75+
private readonly Composition _root;
76+
77+
[OrdinalAttribute(256)]
78+
public Composition()
79+
{
80+
_root = this;
81+
}
82+
83+
internal Composition(Composition parentScope)
84+
{
85+
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
86+
}
87+
88+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
89+
public OtherService<T3> GetMyOtherService_T<T3>()
90+
{
91+
OtherService<T3> transientOtherService0;
92+
IDependency<T3> localDependency97 = new Dependency<T3>();
93+
transientOtherService0 = new OtherService<T3>(localDependency97);
94+
return transientOtherService0;
95+
}
96+
97+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
98+
public Service<T3> GetMyService_T<T3>()
99+
{
100+
return new Service<T3>(new Dependency<T3>());
101+
}
102+
}
103+
```
104+
105+
Class diagram:
106+
107+
```mermaid
108+
---
109+
config:
110+
class:
111+
hideEmptyMembersBox: true
112+
---
113+
classDiagram
114+
DependencyᐸT3ᐳ --|> IDependencyᐸT3ᐳ
115+
Composition ..> OtherServiceᐸT3ᐳ : OtherServiceᐸT3ᐳ GetMyOtherService_TᐸT3ᐳ()
116+
Composition ..> ServiceᐸT3ᐳ : ServiceᐸT3ᐳ GetMyService_TᐸT3ᐳ()
117+
OtherServiceᐸT3ᐳ *-- DependencyᐸT3ᐳ : IDependencyᐸT3ᐳ
118+
ServiceᐸT3ᐳ *-- DependencyᐸT3ᐳ : IDependencyᐸT3ᐳ
119+
namespace Pure.DI.UsageTests.Generics.GenericsRootsScenario {
120+
class Composition {
121+
<<partial>>
122+
+OtherServiceᐸT3ᐳ GetMyOtherService_TᐸT3ᐳ()
123+
+ServiceᐸT3ᐳ GetMyService_TᐸT3ᐳ()
124+
}
125+
class DependencyᐸT3ᐳ {
126+
+Dependency()
127+
}
128+
class IDependencyᐸT3ᐳ {
129+
<<interface>>
130+
}
131+
class OtherServiceᐸT3ᐳ {
132+
}
133+
class ServiceᐸT3ᐳ {
134+
}
135+
}
136+
```
137+

readme/scope.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ partial class Composition: IDisposable
170170
() =>
171171
{
172172
Composition transientComposition3 = this;
173-
Session localValue102 = new Session(transientComposition3);
174-
return localValue102;
173+
Session localValue103 = new Session(transientComposition3);
174+
return localValue103;
175175
});
176176
return new Program(perBlockFunc1);
177177
}

0 commit comments

Comments
 (0)