-
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.
- Loading branch information
1 parent
89cab20
commit a05722e
Showing
22 changed files
with
327 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#### Tag on a method argument | ||
|
||
[](../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>(); | ||
``` | ||
|
||
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 | ||
``` | ||
|
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
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.