-
Notifications
You must be signed in to change notification settings - Fork 22
/
ServiceProviderScenario.cs
64 lines (55 loc) · 1.74 KB
/
ServiceProviderScenario.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
/*
$v=true
$p=99
$d=Service provider
$h=The `// ObjectResolveMethodName = GetService` hint overrides the _object Resolve(Type type)_ method name in _GetService_, allowing the _IServiceProvider_ interface to be implemented in a partial class.
*/
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable CheckNamespace
// ReSharper disable UnusedParameter.Local
// ReSharper disable ArrangeTypeModifiers
// ReSharper disable UnusedMember.Local
// ReSharper disable ArrangeTypeMemberModifiers
namespace Pure.DI.UsageTests.BCL.ServiceProviderScenario;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;
// {
interface IDependency;
class Dependency : IDependency;
interface IService
{
IDependency Dependency { get; }
}
class Service(IDependency dependency) : IService
{
public IDependency Dependency { get; } = dependency;
}
partial class Composition : IServiceProvider
{
static void Setup() =>
DI.Setup()
// The following hint overrides the name of the
// "object Resolve(Type type)" method in "GetService",
// which implements the "IServiceProvider" interface
.Hint(Hint.ObjectResolveMethodName, "GetService")
.Bind<IDependency>().As(Lifetime.Singleton).To<Dependency>()
.Bind<IService>().To<Service>()
.Root<IDependency>()
.Root<IService>();
}
// }
public class Scenario
{
[Fact]
public void Run()
{
// {
var serviceProvider = new Composition();
var service = serviceProvider.GetRequiredService<IService>();
var dependency = serviceProvider.GetRequiredService<IDependency>();
service.Dependency.ShouldBe(dependency);
// }
serviceProvider.SaveClassDiagram();
}
}