Skip to content

Commit cad6259

Browse files
Update README.md
1 parent 0209cff commit cad6259

28 files changed

+212
-155
lines changed

AI_CONTEXT_LARGE.md

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,16 +1209,26 @@ class Service : IService
12091209
// The Dependency attribute specifies to perform an injection,
12101210
// the integer value in the argument specifies
12111211
// the ordinal of injection
1212-
[Dependency] internal IDependency? DependencyVal;
1212+
[Dependency] public IDependency? DependencyVal;
12131213

1214-
public IDependency? Dependency => DependencyVal;
1214+
public IDependency? Dependency
1215+
{
1216+
get
1217+
{
1218+
return DependencyVal;
1219+
}
1220+
}
12151221
}
12161222
```
12171223

12181224
To run the above code, the following NuGet packages must be added:
12191225
- [Pure.DI](https://www.nuget.org/packages/Pure.DI)
12201226
- [Shouldly](https://www.nuget.org/packages/Shouldly)
12211227

1228+
The key points are:
1229+
- The field must be writable
1230+
- The `Dependency` (or `Ordinal`) attribute is used to mark the field for injection
1231+
- The container automatically injects the dependency when resolving the object graph
12221232

12231233
## Method injection
12241234

@@ -1265,6 +1275,10 @@ To run the above code, the following NuGet packages must be added:
12651275
- [Pure.DI](https://www.nuget.org/packages/Pure.DI)
12661276
- [Shouldly](https://www.nuget.org/packages/Shouldly)
12671277

1278+
The key points are:
1279+
- The method must be available to be called from a composition class
1280+
- The `Dependency` (or `Ordinal`) attribute is used to mark the method for injection
1281+
- The container automatically calls the method to inject dependencies
12681282

12691283
## Property injection
12701284

@@ -1299,18 +1313,23 @@ class Service : IService
12991313
// The Dependency attribute specifies to perform an injection,
13001314
// the integer value in the argument specifies
13011315
// the ordinal of injection
1302-
[Dependency]
1303-
public IDependency? Dependency { get; set; }
1316+
[Dependency] public IDependency? Dependency { get; set; }
13041317
}
13051318
```
13061319

13071320
To run the above code, the following NuGet packages must be added:
13081321
- [Pure.DI](https://www.nuget.org/packages/Pure.DI)
13091322
- [Shouldly](https://www.nuget.org/packages/Shouldly)
13101323

1324+
The key points are:
1325+
- The property must be writable
1326+
- The `Dependency` (or `Ordinal`) attribute is used to mark the property for injection
1327+
- The container automatically injects the dependency when resolving the object graph
13111328

13121329
## Default values
13131330

1331+
This example demonstrates how to use default values in dependency injection when explicit injection is not possible.
1332+
13141333
```c#
13151334
using Shouldly;
13161335
using Pure.DI;
@@ -1352,10 +1371,19 @@ To run the above code, the following NuGet packages must be added:
13521371
- [Pure.DI](https://www.nuget.org/packages/Pure.DI)
13531372
- [Shouldly](https://www.nuget.org/packages/Shouldly)
13541373

1374+
The key points are:
1375+
- Default constructor arguments can be used for simple values
1376+
- The DI container will use these defaults if no explicit bindings are provided
1377+
1378+
This example illustrates how to handle default values in a dependency injection scenario:
1379+
- **Constructor Default Argument**: The `Service` class has a constructor with a default value for the name parameter. If no value is provided, “My Service” will be used.
1380+
- **Required Property with Default**: The Dependency property is marked as required but has a default instantiation. This ensures that:
1381+
- The property must be set
1382+
- If no explicit injection occurs, a default value will be used
13551383

13561384
## Required properties or fields
13571385

1358-
All properties or fields marked with the _required_ keyword automatically become injected dependencies.
1386+
This example demonstrates how the `required` modifier can be used to automatically inject dependencies into properties and fields. When a property or field is marked with `required`, the DI will automatically inject the dependency without additional effort.
13591387

13601388
```c#
13611389
using Shouldly;
@@ -1401,6 +1429,7 @@ To run the above code, the following NuGet packages must be added:
14011429
- [Pure.DI](https://www.nuget.org/packages/Pure.DI)
14021430
- [Shouldly](https://www.nuget.org/packages/Shouldly)
14031431

1432+
This approach simplifies dependency injection by eliminating the need to manually configure bindings for required dependencies, making the code more concise and easier to maintain.
14041433

14051434
## Overrides
14061435

@@ -7450,7 +7479,7 @@ internal partial class Composition
74507479

74517480
// Models
74527481
.Bind().To<Log<TT>>()
7453-
.Bind().As(Singleton).To<Clock.Models.Timer>()
7482+
.Bind().As(Singleton).To<Timer>()
74547483
.Bind().As(PerBlock).To<SystemClock>()
74557484

74567485
// Infrastructure
@@ -7516,15 +7545,15 @@ public class App : Application
75167545
desktop.MainWindow = composition.MainWindow;
75177546
break;
75187547

7519-
case ISingleViewApplicationLifetime singleViewPlatform:
7520-
singleViewPlatform.MainView = composition.MainWindow;
7548+
case ISingleViewApplicationLifetime singleView:
7549+
singleView.MainView = composition.MainWindow;
75217550
break;
75227551
}
75237552

75247553
// Handles disposables
7525-
if (ApplicationLifetime is IControlledApplicationLifetime controlledApplicationLifetime)
7554+
if (ApplicationLifetime is IControlledApplicationLifetime controlledLifetime)
75267555
{
7527-
controlledApplicationLifetime.Exit += (_, _) => composition.Dispose();
7556+
controlledLifetime.Exit += (_, _) => composition.Dispose();
75287557
}
75297558
}
75307559

@@ -8660,7 +8689,7 @@ internal partial class Composition
86608689

86618690
// Models
86628691
.Bind().To<Log<TT>>()
8663-
.Bind().To<Clock.Models.Timer>()
8692+
.Bind().To<Timer>()
86648693
.Bind().As(PerBlock).To<SystemClock>()
86658694

86668695
// Infrastructure
@@ -8742,7 +8771,7 @@ internal partial class Composition
87428771

87438772
// Models
87448773
.Bind().To<Log<TT>>()
8745-
.Bind().To<Clock.Models.Timer>()
8774+
.Bind().To<Timer>()
87468775
.Bind().As(PerBlock).To<SystemClock>()
87478776

87488777
// Infrastructure
@@ -8815,7 +8844,7 @@ internal partial class Composition
88158844

88168845
// Models
88178846
.Bind().To<Log<TT>>()
8818-
.Bind().As(Singleton).To<Clock.Models.Timer>()
8847+
.Bind().As(Singleton).To<Timer>()
88198848
.Bind().As(PerBlock).To<SystemClock>()
88208849

88218850
// Infrastructure
@@ -12172,21 +12201,21 @@ DI.Setup("Composition")
1217212201
</blockquote></details>
1217312202

1217412203

12175-
<details><summary>Field UniqueTag</summary><blockquote>
12204+
<details><summary>Field Overrider</summary><blockquote>
1217612205

12177-
Atomically generated smart tag with value "UniqueTag".
12206+
Atomically generated smart tag with value "Overrider".
1217812207
It's used for:
1217912208

12180-
class _Generator__ApiInvocationProcessor_ <-- (UniqueTag) -- _IdGenerator_ as _PerResolve_
12209+
class _Generator__DependencyGraphBuilder_ <-- _IGraphRewriter_(Overrider) -- _GraphOverrider_ as _PerBlock_
1218112210
</blockquote></details>
1218212211

1218312212

12184-
<details><summary>Field Overrider</summary><blockquote>
12213+
<details><summary>Field Cleaner</summary><blockquote>
1218512214

12186-
Atomically generated smart tag with value "Overrider".
12215+
Atomically generated smart tag with value "Cleaner".
1218712216
It's used for:
1218812217

12189-
class _Generator__DependencyGraphBuilder_ <-- _IGraphRewriter_(Overrider) -- _GraphOverrider_ as _PerBlock_
12218+
class _Generator__DependencyGraphBuilder_ <-- _IGraphRewriter_(Cleaner) -- _GraphCleaner_ as _PerBlock_
1219012219
</blockquote></details>
1219112220

1219212221

@@ -12199,46 +12228,46 @@ Atomically generated smart tag with value "GenericType".
1219912228
</blockquote></details>
1220012229

1220112230

12202-
<details><summary>Field Injection</summary><blockquote>
12231+
<details><summary>Field Override</summary><blockquote>
1220312232

12204-
Atomically generated smart tag with value "Injection".
12233+
Atomically generated smart tag with value "Override".
12234+
It's used for:
1220512235

12236+
class _Generator__OverrideIdProvider_ <-- _IIdGenerator_(Override) -- _IdGenerator_ as _PerResolve_
1220612237
</blockquote></details>
1220712238

1220812239

12209-
<details><summary>Field CompositionClass</summary><blockquote>
12240+
<details><summary>Field UsingDeclarations</summary><blockquote>
1221012241

12211-
Atomically generated smart tag with value "CompositionClass".
12242+
Atomically generated smart tag with value "UsingDeclarations".
1221212243
It's used for:
1221312244

12214-
class _Generator__CodeBuilder_ <-- _IBuilder`2_(CompositionClass) -- _CompositionClassBuilder_ as _PerBlock_
12245+
class _Generator__CompositionClassBuilder_ <-- _IBuilder`2_(UsingDeclarations) -- _UsingDeclarationsBuilder_ as _PerBlock_
1221512246
</blockquote></details>
1221612247

1221712248

12218-
<details><summary>Field UsingDeclarations</summary><blockquote>
12249+
<details><summary>Field Injection</summary><blockquote>
1221912250

12220-
Atomically generated smart tag with value "UsingDeclarations".
12221-
It's used for:
12251+
Atomically generated smart tag with value "Injection".
1222212252

12223-
class _Generator__CompositionClassBuilder_ <-- _IBuilder`2_(UsingDeclarations) -- _UsingDeclarationsBuilder_ as _PerBlock_
1222412253
</blockquote></details>
1222512254

1222612255

12227-
<details><summary>Field Override</summary><blockquote>
12256+
<details><summary>Field CompositionClass</summary><blockquote>
1222812257

12229-
Atomically generated smart tag with value "Override".
12258+
Atomically generated smart tag with value "CompositionClass".
1223012259
It's used for:
1223112260

12232-
class _Generator__OverrideIdProvider_ <-- _IIdGenerator_(Override) -- _IdGenerator_ as _PerResolve_
12261+
class _Generator__CodeBuilder_ <-- _IBuilder`2_(CompositionClass) -- _CompositionClassBuilder_ as _PerBlock_
1223312262
</blockquote></details>
1223412263

1223512264

12236-
<details><summary>Field Cleaner</summary><blockquote>
12265+
<details><summary>Field UniqueTag</summary><blockquote>
1223712266

12238-
Atomically generated smart tag with value "Cleaner".
12267+
Atomically generated smart tag with value "UniqueTag".
1223912268
It's used for:
1224012269

12241-
class _Generator__DependencyGraphBuilder_ <-- _IGraphRewriter_(Cleaner) -- _GraphCleaner_ as _PerBlock_
12270+
class _Generator__ApiInvocationProcessor_ <-- (UniqueTag) -- _IdGenerator_ as _PerResolve_
1224212271
</blockquote></details>
1224312272

1224412273

AI_CONTEXT_MEDIUM.md

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,16 +1209,26 @@ class Service : IService
12091209
// The Dependency attribute specifies to perform an injection,
12101210
// the integer value in the argument specifies
12111211
// the ordinal of injection
1212-
[Dependency] internal IDependency? DependencyVal;
1212+
[Dependency] public IDependency? DependencyVal;
12131213

1214-
public IDependency? Dependency => DependencyVal;
1214+
public IDependency? Dependency
1215+
{
1216+
get
1217+
{
1218+
return DependencyVal;
1219+
}
1220+
}
12151221
}
12161222
```
12171223

12181224
To run the above code, the following NuGet packages must be added:
12191225
- [Pure.DI](https://www.nuget.org/packages/Pure.DI)
12201226
- [Shouldly](https://www.nuget.org/packages/Shouldly)
12211227

1228+
The key points are:
1229+
- The field must be writable
1230+
- The `Dependency` (or `Ordinal`) attribute is used to mark the field for injection
1231+
- The container automatically injects the dependency when resolving the object graph
12221232

12231233
## Method injection
12241234

@@ -1265,6 +1275,10 @@ To run the above code, the following NuGet packages must be added:
12651275
- [Pure.DI](https://www.nuget.org/packages/Pure.DI)
12661276
- [Shouldly](https://www.nuget.org/packages/Shouldly)
12671277

1278+
The key points are:
1279+
- The method must be available to be called from a composition class
1280+
- The `Dependency` (or `Ordinal`) attribute is used to mark the method for injection
1281+
- The container automatically calls the method to inject dependencies
12681282

12691283
## Property injection
12701284

@@ -1299,15 +1313,18 @@ class Service : IService
12991313
// The Dependency attribute specifies to perform an injection,
13001314
// the integer value in the argument specifies
13011315
// the ordinal of injection
1302-
[Dependency]
1303-
public IDependency? Dependency { get; set; }
1316+
[Dependency] public IDependency? Dependency { get; set; }
13041317
}
13051318
```
13061319

13071320
To run the above code, the following NuGet packages must be added:
13081321
- [Pure.DI](https://www.nuget.org/packages/Pure.DI)
13091322
- [Shouldly](https://www.nuget.org/packages/Shouldly)
13101323

1324+
The key points are:
1325+
- The property must be writable
1326+
- The `Dependency` (or `Ordinal`) attribute is used to mark the property for injection
1327+
- The container automatically injects the dependency when resolving the object graph
13111328

13121329
## Transient
13131330

@@ -3780,58 +3797,6 @@ To run the above code, the following NuGet packages must be added:
37803797
- [Shouldly](https://www.nuget.org/packages/Shouldly)
37813798

37823799

3783-
## Custom universal attribute
3784-
3785-
You can use a combined attribute, and each method in the list above has an optional parameter that defines the argument number (the default is 0) from where to get the appropriate metadata for _tag_, _ordinal_, or _type_.
3786-
3787-
```c#
3788-
using Shouldly;
3789-
using Pure.DI;
3790-
3791-
DI.Setup(nameof(PersonComposition))
3792-
.TagAttribute<InjectAttribute<TT>>()
3793-
.OrdinalAttribute<InjectAttribute<TT>>(1)
3794-
.TypeAttribute<InjectAttribute<TT>>()
3795-
.Arg<int>("personId")
3796-
.Bind().To(_ => new Uri("https://github.com/DevTeam/Pure.DI"))
3797-
.Bind("NikName").To(_ => "Nik")
3798-
.Bind().To<Person>()
3799-
3800-
// Composition root
3801-
.Root<IPerson>("Person");
3802-
3803-
var composition = new PersonComposition(personId: 123);
3804-
var person = composition.Person;
3805-
person.ToString().ShouldBe("123 Nik https://github.com/DevTeam/Pure.DI");
3806-
3807-
[AttributeUsage(
3808-
AttributeTargets.Constructor
3809-
| AttributeTargets.Method
3810-
| AttributeTargets.Parameter
3811-
| AttributeTargets.Property
3812-
| AttributeTargets.Field)]
3813-
class InjectAttribute<T>(object? tag = null, int ordinal = 0) : Attribute;
3814-
3815-
interface IPerson;
3816-
3817-
class Person([Inject<string>("NikName")] string name) : IPerson
3818-
{
3819-
private object? _state;
3820-
3821-
[Inject<int>(ordinal: 1)] internal object Id = "";
3822-
3823-
public void Initialize([Inject<Uri>] object state) =>
3824-
_state = state;
3825-
3826-
public override string ToString() => $"{Id} {name} {_state}";
3827-
}
3828-
```
3829-
3830-
To run the above code, the following NuGet packages must be added:
3831-
- [Pure.DI](https://www.nuget.org/packages/Pure.DI)
3832-
- [Shouldly](https://www.nuget.org/packages/Shouldly)
3833-
3834-
38353800
## Decorator
38363801

38373802
Interception is the ability to intercept calls between objects in order to enrich or change their behavior, but without having to change their code. A prerequisite for interception is weak binding. That is, if programming is abstraction-based, the underlying implementation can be transformed or improved by "packaging" it into other implementations of the same abstraction. At its core, intercept is an application of the Decorator design pattern. This pattern provides a flexible alternative to inheritance by dynamically "attaching" additional responsibility to an object. Decorator "packs" one implementation of an abstraction into another implementation of the same abstraction like a "matryoshka doll".

0 commit comments

Comments
 (0)