-
Notifications
You must be signed in to change notification settings - Fork 22
/
FieldInjectionScenario.cs
59 lines (48 loc) · 1.39 KB
/
FieldInjectionScenario.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
/*
$v=true
$p=9
$d=Field injection
$h=To use dependency injection for a field, make sure the field is writable and simply add the _Ordinal_ attribute to that field, specifying an ordinal that will be used to determine the injection order:
*/
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable CheckNamespace
// ReSharper disable UnusedParameter.Local
// ReSharper disable ArrangeTypeModifiers
namespace Pure.DI.UsageTests.Basics.FieldInjectionScenario;
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)] internal IDependency? DependencyVal;
public IDependency? Dependency => DependencyVal;
}
// }
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();
}
}