-
Notifications
You must be signed in to change notification settings - Fork 22
/
PerResolveScenario.cs
63 lines (49 loc) · 1.51 KB
/
PerResolveScenario.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
/*
$v=true
$p=2
$d=PerResolve
$h=The _PerResolve_ lifetime ensures that there will be one instance of the dependency for each composition root instance.
*/
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable CheckNamespace
// ReSharper disable ArrangeTypeModifiers
namespace Pure.DI.UsageTests.Lifetimes.PerResolveScenario;
using Xunit;
// {
interface IDependency;
class Dependency : IDependency;
class Service(
IDependency dep1,
IDependency dep2,
(IDependency dep3, IDependency dep4) deps)
{
public IDependency Dep1 { get; } = dep1;
public IDependency Dep2 { get; } = dep2;
public IDependency Dep3 { get; } = deps.dep3;
public IDependency Dep4 { get; } = deps.dep4;
}
// }
public class Scenario
{
[Fact]
public void Run()
{
// {
DI.Setup(nameof(Composition))
// This hint indicates to not generate methods such as Resolve
.Hint(Hint.Resolve, "Off")
.Bind().As(Lifetime.PerResolve).To<Dependency>()
.Bind().As(Lifetime.Singleton).To<(IDependency dep3, IDependency dep4)>()
// Composition root
.Root<Service>("Root");
var composition = new Composition();
var service1 = composition.Root;
service1.Dep1.ShouldBe(service1.Dep2);
service1.Dep3.ShouldBe(service1.Dep4);
service1.Dep1.ShouldBe(service1.Dep3);
var service2 = composition.Root;
service2.Dep1.ShouldNotBe(service1.Dep1);
// }
composition.SaveClassDiagram();
}
}