-
Notifications
You must be signed in to change notification settings - Fork 22
/
OnCannotResolveHintScenario.cs
81 lines (68 loc) · 2.25 KB
/
OnCannotResolveHintScenario.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
/*
$v=true
$p=3
$d=OnCannotResolve hint
$h=Hints are used to fine-tune code generation. The _OnCannotResolve_ hint determines whether to generate a partial `OnCannotResolve<T>(...)` method to handle a scenario where an instance which cannot be resolved.
$h=In addition, setup hints can be comments before the _Setup_ method in the form ```hint = value```, for example: `// OnCannotResolveContractTypeNameRegularExpression = string`.
$f=The `OnCannotResolveContractTypeNameRegularExpression` hint helps define the set of types that require manual dependency resolution. You can use it to specify a regular expression to filter the full type name.
$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 ArrangeTypeModifiers
namespace Pure.DI.UsageTests.Hints.OnCannotResolveHintScenario;
using System.Diagnostics.CodeAnalysis;
using Shouldly;
using Xunit;
// {
using static Hint;
interface IDependency;
class Dependency(string name) : IDependency
{
public override string ToString() => name;
}
interface IService
{
IDependency Dependency { get; }
}
class Service(IDependency dependency) : IService
{
public IDependency Dependency { get; } = dependency;
}
partial class Composition
{
[SuppressMessage("Performance", "CA1822:Mark members as static")]
private partial T OnCannotResolve<T>(
object? tag,
Lifetime lifetime)
{
if (typeof(T) == typeof(string))
{
return (T)(object)"My name";
}
throw new InvalidOperationException("Cannot resolve.");
}
}
// }
public class Scenario
{
[Fact]
public void Run()
{
// Resolve = Off
// {
// OnCannotResolveContractTypeNameRegularExpression = string
DI.Setup(nameof(Composition))
.Hint(OnCannotResolve, "On")
.Bind().To<Dependency>()
.Bind().To<Service>()
.Root<IService>("Root");
var composition = new Composition();
var service = composition.Root;
service.Dependency.ToString().ShouldBe("My name");
// }
composition.SaveClassDiagram();
}
}