-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathTagUniqueScenario.cs
65 lines (53 loc) · 1.59 KB
/
TagUniqueScenario.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
/*
$v=true
$p=4
$d=Tag Unique
$h=`Tag.Unique` is useful to register a binding with a unique tag. It will not be available through the composition root or `Resolve` methods directly, but can be injected in compositions as some kind of enumeration.
$r=Shouldly
*/
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable CheckNamespace
// ReSharper disable ArrangeTypeModifiers
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable UnusedTypeParameter
namespace Pure.DI.UsageTests.Advanced.TagUniqueScenario;
using System.Collections.Immutable;
using Shouldly;
using Xunit;
// {
//# using Pure.DI;
//# using System.Collections.Immutable;
// }
public class Scenario
{
[Fact]
public void Run()
{
// {
DI.Setup(nameof(Composition))
.Bind<IDependency<TT>>(Tag.Unique).To<AbcDependency<TT>>()
.Bind<IDependency<TT>>(Tag.Unique).To<XyzDependency<TT>>()
.Bind<IService<TT>>().To<Service<TT>>()
// Composition root
.Root<IService<string>>("Root");
var composition = new Composition();
var stringService = composition.Root;
stringService.Dependencies.Length.ShouldBe(2);
// }
composition.SaveClassDiagram();
}
}
// {
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];
}
// }