You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: AI_CONTEXT_LARGE.md
+79-71Lines changed: 79 additions & 71 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -317,73 +317,41 @@ For class `Dependency`, the `Bind().To<Dependency>()` binding will be equivalent
317
317
318
318
## Factory
319
319
320
-
This example demonstrates how to create and initialize an instance manually.
321
-
At the compilation stage, the set of dependencies that an object needs in order to be created is determined. In most cases, this happens automatically according to the set of constructors and their arguments and does not require any additional customization efforts. But sometimes it is necessary to manually create an object, as in lines of code:
320
+
This example demonstrates how to create and initialize an instance manually. At the compilation stage, the set of dependencies that the object to be created needs is determined. In most cases, this happens automatically, according to the set of constructors and their arguments, and does not require additional customization efforts. But sometimes it is necessary to manually create and/or initialize an object, as in lines of code:
322
321
323
322
```c#
324
323
usingShouldly;
325
324
usingPure.DI;
326
325
327
326
DI.Setup(nameof(Composition))
328
-
.Bind().To(_=>DateTimeOffset.Now)
329
-
.RootArg<bool>("isFake", "FakeArgTag")
330
327
.Bind<IDependency>().To<IDependency>(ctx=>
331
328
{
332
-
// When building a composition of objects,
333
-
// all of this code will be outside the lambda function.
334
-
335
-
// Some custom logic for creating an instance.
336
-
// For example, here's how you can inject and initialize
This approach is more expensive to maintain, but allows you to create objects more flexibly by passing them some state and introducing dependencies. As in the case of automatic dependency injecting, objects give up control on embedding, and the whole process takes place when the object graph is created.
370
+
There are scenarios where manual control over the creation process is required, such as:
371
+
- When additional initialization logic is needed
372
+
- When complex construction steps are required
373
+
- When specific object states need to be set during creation
374
+
403
375
> [!IMPORTANT]
404
376
> The method `Inject()`cannot be used outside of the binding setup.
405
377
@@ -412,13 +384,13 @@ using Shouldly;
412
384
usingPure.DI;
413
385
414
386
DI.Setup(nameof(Composition))
415
-
.Bind("now datetime").To(_=>DateTimeOffset.Now)
387
+
.Bind("now").To(_=>DateTimeOffset.Now)
416
388
// Injects Dependency and DateTimeOffset instances
417
389
// and performs further initialization logic
418
390
// defined in the lambda function
419
391
.Bind<IDependency>().To((
420
392
Dependencydependency,
421
-
[Tag("now datetime")] DateTimeOffsettime) =>
393
+
[Tag("now")] DateTimeOffsettime) =>
422
394
{
423
395
dependency.Initialize(time);
424
396
returndependency;
@@ -467,6 +439,7 @@ To run the above code, the following NuGet packages must be added:
The example creates a `service` that depends on a `dependency` initialized with a specific timestamp. The `Tag` attribute allows specifying named dependencies for more complex scenarios.
470
443
471
444
## Injection on demand
472
445
@@ -731,7 +704,7 @@ When using composition root arguments, compilation warnings are shown if `Resolv
731
704
732
705
## Tags
733
706
734
-
Sometimes it's important to take control of building a dependency graph. For example, when there are multiple implementations of the same contract. In this case, _tags_ will help:
707
+
Sometimes it's important to take control of building a dependency graph. For example, when there are different implementations of the same interface. In this case, _tags_ will help:
735
708
736
709
```c#
737
710
usingShouldly;
@@ -741,9 +714,7 @@ DI.Setup(nameof(Composition))
741
714
// The `default` tag is used to resolve dependencies
- Use tags to differentiate between implementations
771
+
- Control lifetime management
772
+
- Inject tagged dependencies into constructors
773
+
797
774
The tag can be a constant, a type, a [smart tag](smart-tags.md), or a value of an `Enum` type. The _default_ and _null_ tags are also supported.
798
775
799
776
## Smart tags
@@ -883,7 +860,7 @@ To run the above code, the following NuGet packages must be added:
883
860
884
861
## Build up of an existing object
885
862
886
-
In other words, injecting the necessary dependencies via methods, properties, or fields into an existing object.
863
+
This example demonstrates the Build-Up pattern in dependency injection, where an existing object is injected with necessary dependencies through its properties, methods, or fields.
887
864
888
865
```c#
889
866
usingShouldly;
@@ -918,14 +895,12 @@ interface IDependency
918
895
classDependency : IDependency
919
896
{
920
897
// The Dependency attribute specifies to perform an injection and its order
921
-
[Dependency]
922
-
publicstringName { get; set; } ="";
898
+
[Dependency] publicstringName { get; set; } ="";
923
899
924
900
publicGuidId { get; privateset; } =Guid.Empty;
925
901
926
902
// The Dependency attribute specifies to perform an injection and its order
927
-
[Dependency]
928
-
publicvoidSetId(Guidid) =>Id=id;
903
+
[Dependency] publicvoidSetId(Guidid) =>Id=id;
929
904
}
930
905
931
906
interfaceIService
@@ -940,6 +915,9 @@ To run the above code, the following NuGet packages must be added:
The default builder method name is `BuildUp`. The first argument to this method will always be the instance to be built.
969
+
Important Notes:
970
+
- The default builder method name is `BuildUp`
971
+
- The first argument to the builder method is always the instance to be built
972
+
973
+
Advantages:
974
+
- Allows working with pre-existing objects
975
+
- Provides flexibility in dependency injection
976
+
- Supports partial injection of dependencies
977
+
- Can be used with legacy code
978
+
979
+
Use Cases:
980
+
- When objects are created outside the DI container
981
+
- For working with third-party libraries
982
+
- When migrating existing code to DI
983
+
- For complex object graphs where full construction is not feasible
992
984
993
985
## Builder with arguments
994
986
995
-
Builders can be used with arguments as in the example below:
987
+
This example demonstrates how to use builders with custom arguments in dependency injection. It shows how to pass additional parameters during the build-up process.
996
988
997
989
```c#
998
990
usingShouldly;
@@ -1038,7 +1030,21 @@ To run the above code, the following NuGet packages must be added:
The default builder method name is `BuildUp`. The first argument to this method will always be the instance to be built. The remaining arguments of this method will be listed in the order in which they are defined in the setup.
1033
+
Important Notes:
1034
+
- The default builder method name is `BuildUp`
1035
+
- The first argument to the builder method is always the instance to be built
1036
+
- Additional arguments are passed in the order they are defined in the setup
1037
+
- Root arguments can be used to provide custom values during build-up
1038
+
1039
+
Use Cases:
1040
+
- When additional parameters are required during object construction
1041
+
- For scenarios where dependencies depend on runtime values
1042
+
- When specific initialization data is needed
1043
+
- For conditional injection based on provided arguments
1044
+
1045
+
Best Practices
1046
+
- Keep the number of builder arguments minimal
1047
+
- Use meaningful names for root arguments
1042
1048
1043
1049
## Builders
1044
1050
@@ -1101,7 +1107,9 @@ To run the above code, the following NuGet packages must be added:
0 commit comments