-
Notifications
You must be signed in to change notification settings - Fork 22
/
TagAttributeScenario.cs
68 lines (54 loc) · 1.73 KB
/
TagAttributeScenario.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
$v=true
$p=3
$d=Tag attribute
$h=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:
$f=The tag can be a constant, a type, or a value of an enumerated type. This attribute is part of the API, but you can use your own attribute at any time, and this allows you to define them in the assembly and namespace you want.
*/
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable CheckNamespace
// ReSharper disable UnusedType.Global
// ReSharper disable ArrangeTypeModifiers
namespace Pure.DI.UsageTests.Basics.TagAttributeScenario;
using Shouldly;
using Xunit;
// {
interface IDependency;
class AbcDependency : IDependency;
class XyzDependency : IDependency;
class Dependency : IDependency;
interface IService
{
IDependency Dependency1 { get; }
IDependency Dependency2 { get; }
}
class Service(
[Tag("Abc")] IDependency dependency1,
[Tag("Xyz")] IDependency dependency2)
: IService
{
public IDependency Dependency1 { get; } = dependency1;
public IDependency Dependency2 { get; } = dependency2;
}
// }
public class Scenario
{
[Fact]
public void Run()
{
// Resolve = Off
// {
DI.Setup(nameof(Composition))
.Bind("Abc").To<AbcDependency>()
.Bind("Xyz").To<XyzDependency>()
.Bind().To<Service>()
// Composition root
.Root<IService>("Root");
var composition = new Composition();
var service = composition.Root;
service.Dependency1.ShouldBeOfType<AbcDependency>();
service.Dependency2.ShouldBeOfType<XyzDependency>();
// }
composition.SaveClassDiagram();
}
}