-
Notifications
You must be signed in to change notification settings - Fork 22
/
ConstructorOrdinalAttributeScenario.cs
68 lines (54 loc) · 1.8 KB
/
ConstructorOrdinalAttributeScenario.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=0
$d=Constructor ordinal attribute
$h=When applied to any constructor in a type, automatic injection constructor selection is disabled. The selection will only focus on constructors marked with this attribute, in the appropriate order from smallest value to largest.
$f=The attribute `Ordinal` 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 UnusedParameter.Local
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable UnusedMember.Global
// ReSharper disable ArrangeTypeModifiers
namespace Pure.DI.UsageTests.Attributes.ConstructorOrdinalAttributeScenario;
using Shouldly;
using Xunit;
// {
interface IDependency;
class Dependency : IDependency;
interface IService;
class Service : IService
{
private readonly string _name;
// The integer value in the argument specifies
// the ordinal of injection
[Ordinal(1)]
public Service(IDependency dependency) =>
_name = "with dependency";
[Ordinal(0)]
internal Service(string name) => _name = name;
public Service() => _name = "default";
public override string ToString() => _name;
}
// }
public class Scenario
{
[Fact]
public void Run()
{
// Resolve = Off
// {
DI.Setup(nameof(Composition))
.Arg<string>("serviceName")
.Bind().To<Dependency>()
.Bind().To<Service>()
// Composition root
.Root<IService>("Root");
var composition = new Composition(serviceName: "Xyz");
var service = composition.Root;
service.ToString().ShouldBe("Xyz");
// }
composition.SaveClassDiagram();
}
}