/
ContainerResolver.cs
90 lines (69 loc) · 2.7 KB
/
ContainerResolver.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
87
88
89
90
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
namespace InfinniPlatform.IoC
{
/// <summary>
/// Resolves services registered in IoC-container
/// </summary>
public sealed class ContainerResolver : IContainerResolver
{
/// <summary>
/// Initializes a new instance of <see cref="ContainerResolver" />.
/// </summary>
/// <param name="containerRegistry">Registry for services registered in container.</param>
/// <param name="providerAccessor">Accessors for getting <see cref="IServiceProvider" /> instances in current scope.</param>
public ContainerResolver(IContainerServiceRegistry containerRegistry, IServiceProviderAccessor providerAccessor)
{
_containerRegistry = containerRegistry;
_providerAccessor = providerAccessor;
}
private readonly IContainerServiceRegistry _containerRegistry;
private readonly IServiceProviderAccessor _providerAccessor;
private IServiceProvider ServiceProvider => _providerAccessor.GetProvider();
/// <inheritdoc />
public IEnumerable<Type> Services => _containerRegistry.Services;
/// <inheritdoc />
public bool IsRegistered<TService>() where TService : class
{
return _containerRegistry.IsRegistered<TService>();
}
/// <inheritdoc />
public bool IsRegistered(Type serviceType)
{
return _containerRegistry.IsRegistered(serviceType);
}
/// <inheritdoc />
public bool TryResolve<TService>(out TService serviceInstance) where TService : class
{
serviceInstance = ServiceProvider.GetService<TService>();
return serviceInstance != null;
}
/// <inheritdoc />
public bool TryResolve(Type serviceType, out object serviceInstance)
{
serviceInstance = ServiceProvider.GetService(serviceType);
return serviceInstance != null;
}
/// <inheritdoc />
public TService Resolve<TService>() where TService : class
{
return ServiceProvider.GetRequiredService<TService>();
}
/// <inheritdoc />
public object Resolve(Type serviceType)
{
return ServiceProvider.GetRequiredService(serviceType);
}
/// <inheritdoc />
public TService ResolveOptional<TService>() where TService : class
{
return ServiceProvider.GetService<TService>();
}
/// <inheritdoc />
public object ResolveOptional(Type serviceType)
{
return ServiceProvider.GetService(serviceType);
}
}
}