-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A special tag that allows injection by name
- Loading branch information
1 parent
d45da68
commit a9ff0e1
Showing
19 changed files
with
831 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#### Name tags | ||
|
||
[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](../tests/Pure.DI.UsageTests/Advanced/NameTagsScenario.cs) | ||
|
||
Name tag are ordinary string tag in the special format: the full name of the type member and the name of the target element to be injected, separated by ":". For example for the _MyNamespace_ namespace and the type _Class1_: | ||
|
||
- `MyNamespace.Class1.Class1:state1 - the tag corresponds to the constructor argument named _state_ of type _MyNamespace.Class1_ | ||
- `MyNamespace.Class1.DoSomething:myArg` - the tag corresponds to the _myArg_ argument of the _DoSomething_ method | ||
- `MyNamespace.Class1.MyData` - the tag corresponds to property or field _MyData_ | ||
|
||
>[!NOTE] | ||
>For generic types, the name also contains the number of type parameters, for example, for `IDictionary<TKey, TValue>` the type name would be `IDictionary`2`. | ||
|
||
```c# | ||
interface IDependency; | ||
|
||
class AbcDependency : IDependency; | ||
|
||
class XyzDependency : IDependency; | ||
|
||
class Consumer<T>(IDependency myDep) | ||
{ | ||
public IDependency Dependency { get; } = myDep; | ||
} | ||
|
||
interface IService | ||
{ | ||
IDependency Dependency1 { get; } | ||
|
||
IDependency Dependency2 { get; } | ||
|
||
IDependency Dependency3 { get; } | ||
|
||
IDependency Dependency4 { get; } | ||
} | ||
|
||
class Service( | ||
IDependency dependency1, | ||
IDependency dependency2, | ||
Consumer<string> consumer) | ||
: IService | ||
{ | ||
public IDependency Dependency1 { get; } = dependency1; | ||
|
||
public IDependency Dependency2 { get; } = dependency2; | ||
|
||
public required IDependency Dependency3 { init; get; } | ||
|
||
public IDependency Dependency4 => consumer.Dependency; | ||
} | ||
|
||
DI.Setup(nameof(Composition)) | ||
.Bind( | ||
"MyNamespace.Service.Service:dependency1", | ||
"MyNamespace.Consumer`1.Consumer:myDep") | ||
.To<AbcDependency>() | ||
.Bind("MyNamespace.Service.Service:dependency2", | ||
"MyNamespace.Service:Dependency3") | ||
.To<XyzDependency>() | ||
.Bind().To<Consumer<TT>>() | ||
.Bind<IService>().To<Service>() | ||
|
||
// Specifies to create the composition root named "Root" | ||
.Root<IService>("Root"); | ||
|
||
var composition = new Composition(); | ||
var service = composition.Root; | ||
service.Dependency1.ShouldBeOfType<AbcDependency>(); | ||
service.Dependency2.ShouldBeOfType<XyzDependency>(); | ||
service.Dependency3.ShouldBeOfType<XyzDependency>(); | ||
service.Dependency4.ShouldBeOfType<AbcDependency>(); | ||
``` | ||
|
||
>[!WARNING] | ||
>Although this approach can be useful for specifying exactly what to inject, it can be more expensive to maintain, so it is recommended to use attributes like `[Tag(...)]`. | ||
|
||
Class diagram: | ||
|
||
```mermaid | ||
classDiagram | ||
class Composition { | ||
<<partial>> | ||
+IService Root | ||
} | ||
AbcDependency --|> IDependency : "Pure.DI.UsageTests.Basics.NameTagsScenario.Service.Service:dependency1" | ||
AbcDependency --|> IDependency | ||
class AbcDependency { | ||
+AbcDependency() | ||
} | ||
XyzDependency --|> IDependency : "Pure.DI.UsageTests.Basics.NameTagsScenario.Service.Service:dependency2" | ||
class XyzDependency { | ||
+XyzDependency() | ||
} | ||
Service --|> IService | ||
class Service { | ||
+Service(IDependency dependency1, IDependency dependency2, IDependency dependency3) | ||
} | ||
class IDependency { | ||
<<interface>> | ||
} | ||
class IService { | ||
<<interface>> | ||
} | ||
Composition ..> Service : IService Root | ||
Service *-- "2 " AbcDependency : IDependency | ||
Service o-- "Singleton" XyzDependency : IDependency | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.