-
Notifications
You must be signed in to change notification settings - Fork 22
/
MethodInjectionScenario.cs
61 lines (50 loc) · 1.43 KB
/
MethodInjectionScenario.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
/*
$v=true
$p=9
$d=Method injection
$h=To use dependency implementation for a method, simply add the _Ordinal_ attribute to that method, specifying the sequence number that will be used to define the call to that method:
*/
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable CheckNamespace
// ReSharper disable UnusedParameter.Local
// ReSharper disable ArrangeTypeModifiers
namespace Pure.DI.UsageTests.Basics.MethodInjectionScenario;
using Shouldly;
using Xunit;
// {
interface IDependency;
class Dependency : IDependency;
interface IService
{
IDependency? Dependency { get; }
}
class Service : IService
{
// The Ordinal attribute specifies to perform an injection,
// the integer value in the argument specifies
// the ordinal of injection
[Ordinal(0)]
public void SetDependency(IDependency dependency) =>
Dependency = dependency;
public IDependency? Dependency { get; private set; }
}
// }
public class Scenario
{
[Fact]
public void Run()
{
// Resolve = Off
// {
DI.Setup(nameof(Composition))
.Bind<IDependency>().To<Dependency>()
.Bind<IService>().To<Service>()
// Composition root
.Root<IService>("MyService");
var composition = new Composition();
var service = composition.MyService;
service.Dependency.ShouldBeOfType<Dependency>();
// }
composition.SaveClassDiagram();
}
}