/
inject_to_scope.cs
187 lines (138 loc) · 4.76 KB
/
inject_to_scope.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using Shouldly;
using StructureMap.Testing.Widget;
using Xunit;
namespace Lamar.Testing.IoC.Acceptance;
public class inject_to_scope
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public void inject_into_root_container(bool replace)
{
var container = new Container(_ => { _.Injectable<IWidget>(); });
var widget = new AWidget();
container.Inject(typeof(IWidget), widget, replace);
container.GetInstance<IWidget>()
.ShouldBeSameAs(widget);
container.GetInstance<WidgetUser>()
.Widget.ShouldBeSameAs(widget);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void inject_scoping_with_nested_container(bool replace)
{
var container = new Container(_ => { _.Injectable<IWidget>(); });
var rootWidget = new AWidget();
container.Inject(typeof(IWidget), rootWidget, replace);
var nestedWidget = new BWidget();
var nested = container.GetNestedContainer();
nested.Inject<IWidget>(nestedWidget);
container.GetInstance<IWidget>()
.ShouldBeSameAs(rootWidget);
nested.GetInstance<IWidget>()
.ShouldBeSameAs(nestedWidget);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void requires_assignable_type(bool replace)
{
var container = new Container(_ => { _.Injectable<IWidget>(); });
Assert.Throws<InvalidOperationException>(() => container.Inject(typeof(IWidget), new object(), replace));
}
[Fact]
public void replace_causes_replacement_of_previous_value_on_subsequent_call()
{
var container = new Container(_ => { _.Injectable<IWidget>(); });
var replaced = new AWidget();
var widget = new AWidget();
container.Inject(typeof(IWidget), replaced, false);
container.Inject(typeof(IWidget), widget, true);
container.GetInstance<IWidget>()
.ShouldBeSameAs(widget);
container.GetInstance<WidgetUser>()
.Widget.ShouldBeSameAs(widget);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void generic_calls_nongeneric(bool replace)
{
var container = new Derived(_ => { _.Injectable<IWidget>(); });
var widget = new AWidget();
container.Inject<IWidget>(widget, replace);
container.serviceType.ShouldBeSameAs(typeof(IWidget));
container.@object.ShouldBeSameAs(widget);
container.replace.ShouldBe(replace);
container.GetInstance<IWidget>()
.ShouldBeSameAs(widget);
container.GetInstance<WidgetUser>()
.Widget.ShouldBeSameAs(widget);
}
[Fact]
public void generic_calls_nongeneric_without_replace()
{
var container = new Derived(_ => { _.Injectable<IWidget>(); });
var widget = new AWidget();
container.Inject<IWidget>(widget);
container.serviceType.ShouldBeSameAs(typeof(IWidget));
container.@object.ShouldBeSameAs(widget);
container.replace.ShouldBeFalse();
container.GetInstance<IWidget>()
.ShouldBeSameAs(widget);
container.GetInstance<WidgetUser>()
.Widget.ShouldBeSameAs(widget);
}
[Fact]
public void using_injected_service()
{
#region sample_container-with-injectable
var container = new Container(_ => { _.Injectable<ExecutionContext>(); });
#endregion
#region sample_injecting-context-to-nested
var context = new ExecutionContext();
var nested = container.GetNestedContainer();
nested.Inject(context);
#endregion
#region sample_resolving-using-context
var service = nested.GetInstance<ContextUsingService>();
service.Context.ShouldBeSameAs(context);
#endregion
}
private class Derived : Container
{
public object @object;
public bool replace;
public Type serviceType;
public Derived(Action<ServiceRegistry> configuration) : base(configuration)
{
}
public override void Inject(Type serviceType, object @object, bool replace)
{
this.serviceType = serviceType;
this.@object = @object;
this.replace = replace;
base.Inject(serviceType, @object, replace);
}
}
}
#region sample_ContextUsingService
public class ContextUsingService
{
public ContextUsingService(ExecutionContext context)
{
Context = context;
}
public ExecutionContext Context { get; }
}
#endregion
#region sample_ExecutionContext
// This class is specific to some kind of short lived
// process and lives in a nested container
public class ExecutionContext
{
public Guid Id { get; set; } = Guid.NewGuid();
}
#endregion