forked from roryprimrose/Neovolve.Streamline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyedServices.cs
More file actions
61 lines (50 loc) · 1.56 KB
/
KeyedServices.cs
File metadata and controls
61 lines (50 loc) · 1.56 KB
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
namespace Examples
{
using System.Reflection;
using FluentAssertions;
using NSubstitute;
using Xunit;
public interface IDuplicateService
{
int GetValue();
}
public class KeyedServices
{
private readonly IDuplicateService _firstService;
private readonly IDuplicateService _secondService;
public KeyedServices(IDuplicateService firstService, IDuplicateService secondService)
{
_firstService = firstService;
_secondService = secondService;
}
public int AddValues()
{
var firstValue = _firstService.GetValue();
var secondValue = _secondService.GetValue();
return firstValue + secondValue;
}
}
public class KeyedServicesTests : Tests<KeyedServices>
{
[Fact]
public void CanConfigureKeyedServices()
{
Service<IDuplicateService>("A").GetValue().Returns(3);
Service<IDuplicateService>("B").GetValue().Returns(15);
var actual = SUT.AddValues();
actual.Should().Be(18);
}
protected override object ResolveService(ParameterInfo parameter)
{
if (parameter.ParameterType != typeof(IDuplicateService))
{
return base.ResolveService(parameter);
}
if (parameter.Name == "firstService")
{
return ResolveService(parameter.ParameterType, "A");
}
return ResolveService(parameter.ParameterType, "B");
}
}
}