-
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.
Special tags that allows injection on site
- Loading branch information
1 parent
051a42e
commit 46bcf66
Showing
35 changed files
with
2,479 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#### Tag on a constructor argument | ||
|
||
[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](../tests/Pure.DI.UsageTests/Advanced/TagOnConstructorArgScenario.cs) | ||
|
||
The wildcards ‘*’ and ‘?’ are supported. | ||
|
||
|
||
```c# | ||
namespace Pure.DI.UsageTests.Advanced.OnConstructorArgScenario; | ||
|
||
|
||
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; } | ||
} | ||
|
||
class Service( | ||
IDependency dependency1, | ||
Consumer<string> consumer) | ||
: IService | ||
{ | ||
public IDependency Dependency1 { get; } = dependency1; | ||
|
||
public IDependency Dependency2 => consumer.Dependency; | ||
} | ||
|
||
DI.Setup(nameof(Composition)) | ||
.Bind(Tag.OnConstructorArg<Service>("dependency1")) | ||
.To<AbcDependency>() | ||
.Bind(Tag.OnConstructorArg<Consumer<TT>>("myDep")) | ||
.To<XyzDependency>() | ||
.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>(); | ||
``` | ||
|
||
> [!WARNING] | ||
> Each potentially injectable argument, property, or field contains an additional tag. This tag can be used to specify what can be injected there. This will only work if the binding type and the tag match. So while this approach can be useful for specifying what to enter, it can be more expensive to maintain and less reliable, so it is recommended to use attributes like `[Tag(...)]` instead. | ||
|
||
|
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,102 @@ | ||
#### Tag on a member | ||
|
||
[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](../tests/Pure.DI.UsageTests/Advanced/TagOnMemberScenario.cs) | ||
|
||
The wildcards ‘*’ and ‘?’ are supported. | ||
|
||
|
||
```c# | ||
namespace Pure.DI.UsageTests.Advanced.TagOnMemberScenario; | ||
|
||
|
||
interface IDependency; | ||
|
||
class AbcDependency : IDependency; | ||
|
||
class XyzDependency : IDependency; | ||
|
||
interface IService | ||
{ | ||
IDependency Dependency { get; } | ||
} | ||
|
||
class Service : IService | ||
{ | ||
public required IDependency Dependency { init; get; } | ||
} | ||
|
||
DI.Setup(nameof(Composition)) | ||
.Bind().To<AbcDependency>() | ||
.Bind(Tag.OnMember<Service>(nameof(Service.Dependency))) | ||
.To<XyzDependency>() | ||
.Bind<IService>().To<Service>() | ||
|
||
// Specifies to create the composition root named "Root" | ||
.Root<IService>("Root"); | ||
|
||
var composition = new Composition(); | ||
var service = composition.Root; | ||
service.Dependency.ShouldBeOfType<XyzDependency>(); | ||
``` | ||
|
||
> [!WARNING] | ||
> Each potentially injectable argument, property, or field contains an additional tag. This tag can be used to specify what can be injected there. This will only work if the binding type and the tag match. So while this approach can be useful for specifying what to enter, it can be more expensive to maintain and less reliable, so it is recommended to use attributes like `[Tag(...)]` instead. | ||
The following partial class will be generated: | ||
|
||
```c# | ||
partial class Composition | ||
{ | ||
private readonly Composition _root; | ||
|
||
public Composition() | ||
{ | ||
_root = this; | ||
} | ||
|
||
internal Composition(Composition parentScope) | ||
{ | ||
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root; | ||
} | ||
|
||
public IService Root | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get | ||
{ | ||
return new Service() | ||
{ | ||
Dependency = new XyzDependency() | ||
}; | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Class diagram: | ||
|
||
```mermaid | ||
classDiagram | ||
class Composition { | ||
<<partial>> | ||
+IService Root | ||
} | ||
XyzDependency --|> IDependency | ||
class XyzDependency { | ||
+XyzDependency() | ||
} | ||
Service --|> IService | ||
class Service { | ||
+Service() | ||
+IDependency Dependency | ||
} | ||
class IDependency { | ||
<<interface>> | ||
} | ||
class IService { | ||
<<interface>> | ||
} | ||
Composition ..> Service : IService Root | ||
Service *-- 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#### Tag on a method argument | ||
|
||
[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](../tests/Pure.DI.UsageTests/Advanced/TagOnMethodArgScenario.cs) | ||
|
||
The wildcards ‘*’ and ‘?’ are supported. | ||
|
||
|
||
```c# | ||
namespace Pure.DI.UsageTests.Advanced.TagOnMethodArgScenario; | ||
|
||
|
||
interface IDependency; | ||
|
||
class AbcDependency : IDependency; | ||
|
||
class XyzDependency : IDependency; | ||
|
||
interface IService | ||
{ | ||
IDependency? Dependency { get; } | ||
} | ||
|
||
class Service : IService | ||
{ | ||
[Ordinal(1)] | ||
public void Initialize(IDependency dep) => | ||
Dependency = dep; | ||
|
||
public IDependency? Dependency { get; private set; } | ||
} | ||
|
||
DI.Setup(nameof(Composition)) | ||
.Bind().To<AbcDependency>() | ||
.Bind(Tag.OnMethodArg<Service>(nameof(Service.Initialize), "dep")) | ||
.To<XyzDependency>() | ||
.Bind<IService>().To<Service>() | ||
|
||
// Specifies to create the composition root named "Root" | ||
.Root<IService>("Root"); | ||
|
||
var composition = new Composition(); | ||
var service = composition.Root; | ||
service.Dependency.ShouldBeOfType<XyzDependency>(); | ||
``` | ||
|
||
> [!WARNING] | ||
> Each potentially injectable argument, property, or field contains an additional tag. This tag can be used to specify what can be injected there. This will only work if the binding type and the tag match. So while this approach can be useful for specifying what to enter, it can be more expensive to maintain and less reliable, so it is recommended to use attributes like `[Tag(...)]` instead. | ||
The following partial class will be generated: | ||
|
||
```c# | ||
partial class Composition | ||
{ | ||
private readonly Composition _root; | ||
|
||
public Composition() | ||
{ | ||
_root = this; | ||
} | ||
|
||
internal Composition(Composition parentScope) | ||
{ | ||
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root; | ||
} | ||
|
||
public IService Root | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get | ||
{ | ||
Service transientService0 = new Service(); | ||
transientService0.Initialize(new XyzDependency()); | ||
return transientService0; | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Class diagram: | ||
|
||
```mermaid | ||
classDiagram | ||
class Composition { | ||
<<partial>> | ||
+IService Root | ||
} | ||
XyzDependency --|> IDependency | ||
class XyzDependency { | ||
+XyzDependency() | ||
} | ||
Service --|> IService | ||
class Service { | ||
+Service() | ||
+Initialize(IDependency dep) : Void | ||
} | ||
class IDependency { | ||
<<interface>> | ||
} | ||
class IService { | ||
<<interface>> | ||
} | ||
Composition ..> Service : IService Root | ||
Service *-- XyzDependency : IDependency | ||
``` | ||
|
Oops, something went wrong.