Skip to content

Commit 8da76f0

Browse files
Simplifying samples
1 parent 8cc2148 commit 8da76f0

File tree

181 files changed

+313
-4338
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+313
-4338
lines changed

AI_CONTEXT_LARGE.md

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ This can be done if these methods are not needed, in case only certain compositi
164164

165165
## Resolve methods
166166

167-
This example shows how to resolve the composition roots using the _Resolve_ methods by _Service Locator_ approach. `Resolve` methods are generated automatically for each registered root.
167+
This example shows how to resolve the roots of a composition using `Resolve` methods to use the composition as a _Service Locator_. The `Resolve` methods are generated automatically without additional effort.
168168

169169
```c#
170170
using Pure.DI;
@@ -209,13 +209,10 @@ class OtherService : IService;
209209
To run the above code, the following NuGet package must be added:
210210
- [Pure.DI](https://www.nuget.org/packages/Pure.DI)
211211

212-
_Resolve_ methods are similar to calls to the roots of a composition. Composition roots are common properties. Their use is efficient and does not cause exceptions. And that is why it is recommended to use them. In contrast, _Resolve_ methods have a number of disadvantages:
213-
214-
- They provide access to an unlimited set of dependencies.
215-
216-
- Their use can potentially lead to runtime exceptions, for example, when the corresponding root has not been defined.
217-
218-
- Lead to performance degradation because they search for the root of a composition based on its type.
212+
_Resolve_ methods are similar to calls to composition roots. Composition roots are properties (or methods). Their use is efficient and does not cause exceptions. This is why they are recommended to be used. In contrast, _Resolve_ methods have a number of disadvantages:
213+
- They provide access to an unlimited set of dependencies (_Service Locator_).
214+
- Their use can potentially lead to runtime exceptions. For example, when the corresponding root has not been defined.
215+
- Sometimes cannot be used directly, e.g., for MAUI/WPF/Avalonia binding.
219216

220217
## Simplified binding
221218

@@ -1649,8 +1646,8 @@ class Service : IService
16491646
partial class Composition
16501647
{
16511648
private void Setup() =>
1652-
DI.Setup(nameof(Composition))
16531649

1650+
DI.Setup(nameof(Composition))
16541651
.Arg<Serilog.ILogger>("logger", "from arg")
16551652
.Bind().To(ctx =>
16561653
{
@@ -1742,8 +1739,6 @@ using Pure.DI;
17421739
using static Pure.DI.Lifetime;
17431740

17441741
DI.Setup(nameof(Composition))
1745-
// This hint indicates to not generate methods such as Resolve
1746-
.Hint(Hint.Resolve, "Off")
17471742
.Bind().As(Transient).To<Dependency>()
17481743
.Bind().To<Service>()
17491744
.Root<IService>("Root");
@@ -1801,8 +1796,6 @@ using Pure.DI;
18011796
using static Pure.DI.Lifetime;
18021797

18031798
DI.Setup(nameof(Composition))
1804-
// This hint indicates to not generate methods such as Resolve
1805-
.Hint(Hint.Resolve, "Off")
18061799
.Bind().As(Singleton).To<Dependency>()
18071800
.Bind().To<Service>()
18081801
.Root<IService>("Root");
@@ -1863,8 +1856,6 @@ using Pure.DI;
18631856
using static Pure.DI.Lifetime;
18641857

18651858
DI.Setup(nameof(Composition))
1866-
// This hint indicates to not generate methods such as Resolve
1867-
.Hint(Hint.Resolve, "Off")
18681859
.Bind().As(PerResolve).To<Dependency>()
18691860
.Bind().As(Singleton).To<(IDependency dep3, IDependency dep4)>()
18701861

@@ -1915,8 +1906,6 @@ using Pure.DI;
19151906
using static Pure.DI.Lifetime;
19161907

19171908
DI.Setup(nameof(Composition))
1918-
// This hint indicates to not generate methods such as Resolve
1919-
.Hint(Hint.Resolve, "Off")
19201909
.Bind().As(PerBlock).To<Dependency>()
19211910
.Bind().As(Singleton).To<(IDependency dep3, IDependency dep4)>()
19221911

@@ -2027,9 +2016,8 @@ partial class Program(Func<Session> sessionFactory)
20272016
partial class Composition
20282017
{
20292018
static void Setup() =>
2019+
20302020
DI.Setup()
2031-
// This hint indicates to not generate methods such as Resolve
2032-
.Hint(Hint.Resolve, "Off")
20332021
.Bind().As(Scoped).To<Dependency>()
20342022
.Bind().To<Service>()
20352023

@@ -2090,9 +2078,8 @@ partial class Program(Func<IService> serviceFactory)
20902078
partial class Composition
20912079
{
20922080
static void Setup() =>
2081+
20932082
DI.Setup()
2094-
// This hint indicates to not generate methods such as Resolve
2095-
.Hint(Hint.Resolve, "Off")
20962083
.Bind().As(Scoped).To<Dependency>()
20972084
// Session composition root
20982085
.Root<Service>("SessionRoot", kind: RootKinds.Private)
@@ -2101,7 +2088,7 @@ partial class Composition
21012088
{
21022089
// Creates a new scope from the parent scope
21032090
var scope = new Composition(parentScope);
2104-
// Provides a scope root
2091+
// Provides the session root in a new scope
21052092
return scope.SessionRoot;
21062093
})
21072094

@@ -2127,8 +2114,6 @@ using Pure.DI;
21272114
using static Pure.DI.Lifetime;
21282115

21292116
DI.Setup(nameof(Composition))
2130-
// This hint indicates to not generate methods such as Resolve
2131-
.Hint(Hint.Resolve, "Off")
21322117
// Default Lifetime applies
21332118
// to all bindings until the end of the chain
21342119
// or the next call to the DefaultLifetime method
@@ -2181,8 +2166,6 @@ using Pure.DI;
21812166
using static Pure.DI.Lifetime;
21822167

21832168
DI.Setup(nameof(Composition))
2184-
// This hint indicates to not generate methods such as Resolve
2185-
.Hint(Hint.Resolve, "Off")
21862169
// Default lifetime applied to a specific type
21872170
.DefaultLifetime<IDependency>(Singleton)
21882171
.Bind().To<Dependency>()
@@ -2233,8 +2216,6 @@ using Pure.DI;
22332216
using static Pure.DI.Lifetime;
22342217

22352218
DI.Setup(nameof(Composition))
2236-
// This hint indicates to not generate methods such as Resolve
2237-
.Hint(Hint.Resolve, "Off")
22382219
// Default lifetime applied to a specific type
22392220
.DefaultLifetime<IDependency>(Singleton, "some tag")
22402221
.Bind("some tag").To<Dependency>()
@@ -2340,8 +2321,6 @@ using Pure.DI;
23402321
using static Pure.DI.Lifetime;
23412322

23422323
DI.Setup(nameof(Composition))
2343-
// This hint indicates to not generate methods such as Resolve
2344-
.Hint(Hint.Resolve, "Off")
23452324
.Bind().As(Singleton).To<Dependency>()
23462325
.Bind().To<Service>()
23472326
.Root<IService>("Root");
@@ -2459,9 +2438,8 @@ partial class Program(Func<Session> sessionFactory)
24592438
partial class Composition
24602439
{
24612440
static void Setup() =>
2441+
24622442
DI.Setup()
2463-
// This hint indicates to not generate methods such as Resolve
2464-
.Hint(Hint.Resolve, "Off")
24652443
.Bind().As(Scoped).To<Dependency>()
24662444
.Bind().To<Service>()
24672445

@@ -2845,7 +2823,6 @@ By default, tasks are started automatically when they are injected. But you can
28452823
using Pure.DI;
28462824

28472825
DI.Setup(nameof(Composition))
2848-
.Hint(Hint.Resolve, "Off")
28492826
// Overrides the default binding that performs an auto-start of a task
28502827
// when it is created. This binding will simply create the task.
28512828
// The start will be handled by the consumer.
@@ -6443,6 +6420,7 @@ public partial class Composition
64436420
// In fact, this method will not be called at runtime
64446421
[Conditional("DI")]
64456422
void Setup() =>
6423+
64466424
DI.Setup()
64476425
.Bind<IDependency>().To<Dependency>()
64486426
.Bind<long>().To(_ => GenerateId())
@@ -6478,6 +6456,7 @@ class Service(IDependency dependency) : IService;
64786456

64796457
partial class Composition
64806458
{
6459+
64816460
// This method will not be called in runtime
64826461
static void Setup1() =>
64836462
DI.Setup()
@@ -6556,6 +6535,7 @@ class Service(IDependency dependency) : IService
65566535
partial class Composition
65576536
{
65586537
static void Setup() =>
6538+
65596539
DI.Setup()
65606540
.Bind().To<Dependency>()
65616541
.Bind().To<Service>()
@@ -6627,6 +6607,7 @@ class Service(Func<Owned<IDependency>> dependencyFactory)
66276607
partial class Composition
66286608
{
66296609
static void Setup() =>
6610+
66306611
DI.Setup()
66316612
.Bind().To<Dependency>()
66326613
.Bind().To<Service>()
@@ -6727,6 +6708,7 @@ class Service(
67276708
partial class Composition
67286709
{
67296710
static void Setup() =>
6711+
67306712
DI.Setup()
67316713
.Bind().To<Dependency>()
67326714
.Bind("single").As(Lifetime.Singleton).To<Dependency>()
@@ -6825,6 +6807,7 @@ class Service(
68256807
partial class Composition
68266808
{
68276809
static void Setup() =>
6810+
68286811
DI.Setup()
68296812
.Bind().To<Dependency>()
68306813
.Bind("single").As(Lifetime.Singleton).To<Dependency>()
@@ -6895,8 +6878,8 @@ class Service(IDependency dependency) : IService
68956878
partial class Composition
68966879
{
68976880
static void Setup() =>
6898-
DI.Setup()
68996881

6882+
DI.Setup()
69006883
.Bind().To<Dependency>()
69016884
.Bind().To<Service>()
69026885

@@ -6974,6 +6957,7 @@ class Service(Func<Owned<IDependency>> dependencyFactory)
69746957
partial class Composition
69756958
{
69766959
static void Setup() =>
6960+
69776961
DI.Setup()
69786962
.Bind<IDependency>().To<Dependency>()
69796963
.Bind().To<Service>()
@@ -7123,7 +7107,6 @@ using Pure.DI;
71237107
using OtherAssembly;
71247108

71257109
DI.Setup(nameof(Composition))
7126-
.Hint(Hint.Resolve, "Off")
71277110
// Binds to exposed composition roots from other project
71287111
.RootArg<CompositionInOtherProject>("baseComposition")
71297112
.Root<Program>("GetProgram");
@@ -7207,7 +7190,6 @@ using static Pure.DI.Lifetime;
72077190
using OtherAssembly;
72087191

72097192
DI.Setup(nameof(Composition))
7210-
.Hint(Hint.Resolve, "Off")
72117193
.RootArg<int>("id")
72127194
// Binds to exposed composition roots from other project
72137195
.Bind().As(Singleton).To<CompositionWithGenericRootsAndArgsInOtherProject>()
@@ -7262,8 +7244,8 @@ class Service : IService
72627244
partial class Composition
72637245
{
72647246
private void Setup() =>
7265-
DI.Setup(nameof(Composition))
72667247

7248+
DI.Setup(nameof(Composition))
72677249
.Hint(Hint.OnNewInstance, "On")
72687250
.Hint(Hint.OnDependencyInjection, "On")
72697251
// Excluding loggers
@@ -12240,21 +12222,21 @@ DI.Setup("Composition")
1224012222
</blockquote></details>
1224112223

1224212224

12243-
<details><summary>Field GenericType</summary><blockquote>
12225+
<details><summary>Field UniqueTag</summary><blockquote>
1224412226

12245-
Atomically generated smart tag with value "GenericType".
12227+
Atomically generated smart tag with value "UniqueTag".
1224612228
It's used for:
1224712229

12248-
class _Generator__TypeResolver_ <-- _IIdGenerator_(GenericType) -- _IdGenerator_ as _PerResolve_
12230+
class _Generator__ApiInvocationProcessor_ <-- (UniqueTag) -- _IdGenerator_ as _PerResolve_
1224912231
</blockquote></details>
1225012232

1225112233

12252-
<details><summary>Field Overrider</summary><blockquote>
12234+
<details><summary>Field GenericType</summary><blockquote>
1225312235

12254-
Atomically generated smart tag with value "Overrider".
12236+
Atomically generated smart tag with value "GenericType".
1225512237
It's used for:
1225612238

12257-
class _Generator__DependencyGraphBuilder_ <-- _IGraphRewriter_(Overrider) -- _GraphOverrider_ as _PerBlock_
12239+
class _Generator__TypeResolver_ <-- _IIdGenerator_(GenericType) -- _IdGenerator_ as _PerResolve_
1225812240
</blockquote></details>
1225912241

1226012242

@@ -12274,30 +12256,30 @@ Atomically generated smart tag with value "Override".
1227412256
</blockquote></details>
1227512257

1227612258

12277-
<details><summary>Field Cleaner</summary><blockquote>
12259+
<details><summary>Field CompositionClass</summary><blockquote>
1227812260

12279-
Atomically generated smart tag with value "Cleaner".
12261+
Atomically generated smart tag with value "CompositionClass".
1228012262
It's used for:
1228112263

12282-
class _Generator__DependencyGraphBuilder_ <-- _IGraphRewriter_(Cleaner) -- _GraphCleaner_ as _PerBlock_
12264+
class _Generator__CodeBuilder_ <-- _IBuilder`2_(CompositionClass) -- _CompositionClassBuilder_ as _PerBlock_
1228312265
</blockquote></details>
1228412266

1228512267

12286-
<details><summary>Field UniqueTag</summary><blockquote>
12268+
<details><summary>Field Overrider</summary><blockquote>
1228712269

12288-
Atomically generated smart tag with value "UniqueTag".
12270+
Atomically generated smart tag with value "Overrider".
1228912271
It's used for:
1229012272

12291-
class _Generator__ApiInvocationProcessor_ <-- (UniqueTag) -- _IdGenerator_ as _PerResolve_
12273+
class _Generator__DependencyGraphBuilder_ <-- _IGraphRewriter_(Overrider) -- _GraphOverrider_ as _PerBlock_
1229212274
</blockquote></details>
1229312275

1229412276

12295-
<details><summary>Field CompositionClass</summary><blockquote>
12277+
<details><summary>Field Cleaner</summary><blockquote>
1229612278

12297-
Atomically generated smart tag with value "CompositionClass".
12279+
Atomically generated smart tag with value "Cleaner".
1229812280
It's used for:
1229912281

12300-
class _Generator__CodeBuilder_ <-- _IBuilder`2_(CompositionClass) -- _CompositionClassBuilder_ as _PerBlock_
12282+
class _Generator__DependencyGraphBuilder_ <-- _IGraphRewriter_(Cleaner) -- _GraphCleaner_ as _PerBlock_
1230112283
</blockquote></details>
1230212284

1230312285

0 commit comments

Comments
 (0)