-
Notifications
You must be signed in to change notification settings - Fork 22
/
GenericsCompositionRootsScenario.cs
80 lines (66 loc) · 2.54 KB
/
GenericsCompositionRootsScenario.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
/*
$v=true
$p=2
$d=Generic composition roots
$h=Sometimes you want to be able to create composition roots with type parameters. In this case, the composition root can only be represented by a method.
$h=> [!IMPORTANT]
$h=> `Resolve()' methods cannot be used to resolve generic composition roots.
$f=> [!IMPORTANT]
$f=> The method `Inject()`cannot be used outside of the binding setup.
*/
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable CheckNamespace
// ReSharper disable UnusedParameter.Local
// ReSharper disable ArrangeTypeModifiers
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable UnusedVariable
// ReSharper disable UnusedTypeParameter
#pragma warning disable CS9113 // Parameter is unread.
namespace Pure.DI.UsageTests.Generics.GenericsCompositionRootsScenario;
using Shouldly;
using Xunit;
// {
interface IDependency<T>;
class Dependency<T> : IDependency<T>;
interface IService<T>;
class Service<T>(IDependency<T> dependency) : IService<T>;
class OtherService<T>(IDependency<T> dependency) : IService<T>;
// }
public class Scenario
{
[Fact]
public void Run()
{
// {
DI.Setup(nameof(Composition))
// This hint indicates to not generate methods such as Resolve
.Hint(Hint.Resolve, "Off")
.Bind().To<Dependency<TT>>()
.Bind().To<Service<TT>>()
// Creates OtherService manually,
// just for the sake of example
.Bind("Other").To(ctx =>
{
ctx.Inject(out IDependency<TT> dependency);
return new OtherService<TT>(dependency);
})
// Specifies to create a regular public method
// to get a composition root of type Service<T>
// with the name "GetMyRoot"
.Root<IService<TT>>("GetMyRoot")
// Specifies to create a regular public method
// to get a composition root of type OtherService<T>
// with the name "GetOtherService"
// using the "Other" tag
.Root<IService<TT>>("GetOtherService", "Other");
var composition = new Composition();
// service = new Service<int>(new Dependency<int>());
var service = composition.GetMyRoot<int>();
// someOtherService = new OtherService<int>(new Dependency<int>());
var someOtherService = composition.GetOtherService<string>();
// }
service.ShouldBeOfType<Service<int>>();
someOtherService.ShouldBeOfType<OtherService<string>>();
composition.SaveClassDiagram();
}
}