/
enumerable_instances.cs
91 lines (76 loc) · 2.81 KB
/
enumerable_instances.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
using Shouldly;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace StructureMap.Testing.Acceptance
{
public class enumerable_instances
{
#region sample_EnumerableFamilyPolicy_in_action
[Fact]
public void collection_types_are_all_possible_by_default()
{
// NOTE that we do NOT make any explicit registration of
// IList<IWidget>, IEnumerable<IWidget>, ICollection<IWidget>, or IWidget[]
var container = new Container(_ =>
{
_.For<IWidget>().Add<AWidget>();
_.For<IWidget>().Add<BWidget>();
_.For<IWidget>().Add<CWidget>();
});
// IList<T>
container.GetInstance<IList<IWidget>>()
.Select(x => x.GetType())
.ShouldHaveTheSameElementsAs(typeof(AWidget), typeof(BWidget), typeof(CWidget));
// ICollection<T>
container.GetInstance<ICollection<IWidget>>()
.Select(x => x.GetType())
.ShouldHaveTheSameElementsAs(typeof(AWidget), typeof(BWidget), typeof(CWidget));
// Array of T
container.GetInstance<IWidget[]>()
.Select(x => x.GetType())
.ShouldHaveTheSameElementsAs(typeof(AWidget), typeof(BWidget), typeof(CWidget));
}
#endregion
#region sample_explicit-enumeration-behavior
[Fact]
public void override_enumeration_behavior()
{
var container = new Container(_ =>
{
_.For<IWidget>().Add<AWidget>();
_.For<IWidget>().Add<BWidget>();
_.For<IWidget>().Add<CWidget>();
// Explicit registration should have precedence over the default
// behavior
_.For<IWidget[]>().Use(new IWidget[] { new DefaultWidget() });
});
container.GetInstance<IWidget[]>()
.Single().ShouldBeOfType<DefaultWidget>();
}
#endregion
#region sample_IWidgetValidator-enumerable
public interface IWidgetValidator
{
IEnumerable<string> Validate(IWidget widget);
}
public class WidgetProcessor
{
private readonly IEnumerable<IWidgetValidator> _validators;
public WidgetProcessor(IEnumerable<IWidgetValidator> validators)
{
_validators = validators;
}
public void Process(IWidget widget)
{
var validationMessages = _validators.SelectMany(x => x.Validate(widget))
.ToArray();
if (validationMessages.Any())
{
// don't process the widget
}
}
}
#endregion
}
}