-
Notifications
You must be signed in to change notification settings - Fork 22
/
OnDependencyInjectionHintScenario.cs
86 lines (72 loc) · 2.39 KB
/
OnDependencyInjectionHintScenario.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
/*
$v=true
$p=2
$d=OnDependencyInjection hint
$h=Hints are used to fine-tune code generation. The _OnDependencyInjection_ hint determines whether to generate partial _OnDependencyInjection_ method to control of dependency injection.
$h=In addition, setup hints can be comments before the _Setup_ method in the form ```hint = value```, for example: `// OnDependencyInjection = On`.
$f=The `OnDependencyInjectionContractTypeNameRegularExpression` hint helps identify the set of types that require injection control. You can use it to specify a regular expression to filter the full name of a type.
$f=For more hints, see [this](README.md#setup-hints) page.
*/
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable CheckNamespace
// ReSharper disable UnusedParameterInPartialMethod
// ReSharper disable UnusedVariable
// ReSharper disable UnusedMemberInSuper.Global
// ReSharper disable ArrangeTypeModifiers
// ReSharper disable UnusedMember.Global
// ReSharper disable NotAccessedPositionalProperty.Global
namespace Pure.DI.UsageTests.Hints.OnDependencyInjectionHintScenario;
using Shouldly;
using Xunit;
// {
using static Hint;
interface IDependency;
record Dependency(int Id) : IDependency;
interface IService
{
IDependency Dependency { get; }
}
class Service(IDependency dependency) : IService
{
public IDependency Dependency { get; } = dependency;
}
partial class Composition
{
private readonly List<string> _log = [];
public Composition(List<string> log) : this() =>
_log = log;
private partial T OnDependencyInjection<T>(
in T value,
object? tag,
Lifetime lifetime)
{
_log.Add($"{value?.GetType().Name} injected");
return value;
}
}
// }
public class Scenario
{
[Fact]
public void Run()
{
// Resolve = Off
// {
// OnDependencyInjection = On
DI.Setup(nameof(Composition))
.Hint(OnDependencyInjectionContractTypeNameRegularExpression, "(.*IDependency|int)$")
.RootArg<int>("id")
.Bind().To<Dependency>()
.Bind().To<Service>()
.Root<IService>("GetRoot");
var log = new List<string>();
var composition = new Composition(log);
var service = composition.GetRoot(33);
log.ShouldBe([
"Int32 injected",
"Dependency injected"
]);
// }
composition.SaveClassDiagram();
}
}