-
Notifications
You must be signed in to change notification settings - Fork 22
/
EnumerableGenericsScenario.cs
67 lines (54 loc) · 1.75 KB
/
EnumerableGenericsScenario.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
/*
$v=true
$p=1
$d=Enumerable generics
*/
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable CheckNamespace
// ReSharper disable ArrangeTypeModifiers
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable UnusedTypeParameter
namespace Pure.DI.UsageTests.BCL.EnumerableGenericsScenario;
using System.Collections.Immutable;
using Shouldly;
using Xunit;
// {
interface IDependency<T>;
class AbcDependency<T> : IDependency<T>;
class XyzDependency<T> : IDependency<T>;
interface IService<T>
{
ImmutableArray<IDependency<T>> Dependencies { get; }
}
class Service<T>(IEnumerable<IDependency<T>> dependencies) : IService<T>
{
public ImmutableArray<IDependency<T>> Dependencies { get; }
= [..dependencies];
}
// }
public class Scenario
{
[Fact]
public void Run()
{
// {
DI.Setup(nameof(Composition))
.Bind<IDependency<TT>>().To<AbcDependency<TT>>()
.Bind<IDependency<TT>>("Xyz").To<XyzDependency<TT>>()
.Bind<IService<TT>>().To<Service<TT>>()
// Composition roots
.Root<IService<int>>("IntRoot")
.Root<IService<string>>("StringRoot");
var composition = new Composition();
var intService = composition.IntRoot;
intService.Dependencies.Length.ShouldBe(2);
intService.Dependencies[0].ShouldBeOfType<AbcDependency<int>>();
intService.Dependencies[1].ShouldBeOfType<XyzDependency<int>>();
var stringService = composition.StringRoot;
stringService.Dependencies.Length.ShouldBe(2);
stringService.Dependencies[0].ShouldBeOfType<AbcDependency<string>>();
stringService.Dependencies[1].ShouldBeOfType<XyzDependency<string>>();
// }
composition.SaveClassDiagram();
}
}