-
Notifications
You must be signed in to change notification settings - Fork 22
/
CompositionRootKindsScenario.cs
74 lines (59 loc) · 1.82 KB
/
CompositionRootKindsScenario.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
/*
$v=true
$p=1
$d=Composition root kinds
*/
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable CheckNamespace
// ReSharper disable UnusedParameter.Local
// ReSharper disable ArrangeTypeModifiers
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable UnusedVariable
// ReSharper disable UnusedMember.Local
// ReSharper disable ArrangeTypeMemberModifiers
namespace Pure.DI.UsageTests.Advanced.CompositionRootKindsScenario;
using Shouldly;
using Xunit;
// {
interface IDependency;
class Dependency : IDependency;
interface IService;
class Service : IService
{
public Service(IDependency dependency)
{
}
}
class OtherService : IService;
partial class Composition
{
void Setup() =>
DI.Setup(nameof(Composition))
.Bind<IService>().To<Service>()
.Bind<IService>("Other").To<OtherService>()
.Bind<IDependency>().To<Dependency>()
// Creates a public root method named "GetOtherService"
.Root<IService>("GetOtherService", "Other", RootKinds.Public | RootKinds.Method)
// Creates a private partial root method named "GetRoot"
.Root<IService>("GetRoot", kind: RootKinds.Private | RootKinds.Partial | RootKinds.Method)
// Creates a internal static root named "Dependency"
.Root<IDependency>("Dependency", kind: RootKinds.Internal | RootKinds.Static);
private partial IService GetRoot();
public IService Root => GetRoot();
}
// }
public class Scenario
{
[Fact]
public void Run()
{
// {
var composition = new Composition();
var service = composition.Root;
var otherService = composition.GetOtherService();
var dependency = Composition.Dependency;
// }
service.ShouldBeOfType<Service>();
composition.SaveClassDiagram();
}
}