forked from dnnsoftware/Dnn.Platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerWithServiceProviderFallback.cs
More file actions
165 lines (129 loc) · 6.74 KB
/
ContainerWithServiceProviderFallback.cs
File metadata and controls
165 lines (129 loc) · 6.74 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
namespace DotNetNuke.ComponentModel;
using System;
using System.Collections;
using DotNetNuke.Instrumentation;
using Microsoft.Extensions.DependencyInjection;
/// <summary>A container which gets components from an <see cref="IServiceProvider"/> for components not registered.</summary>
public class ContainerWithServiceProviderFallback : IContainer
{
private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(ContainerWithServiceProviderFallback));
private readonly IContainer container;
private readonly IServiceProvider serviceProvider;
/// <summary>Initializes a new instance of the <see cref="ContainerWithServiceProviderFallback"/> class.</summary>
/// <param name="container">The container to wrap.</param>
/// <param name="serviceProvider">The service provider.</param>
public ContainerWithServiceProviderFallback(IContainer container, IServiceProvider serviceProvider)
{
this.container = container;
this.serviceProvider = serviceProvider;
}
/// <inheritdoc />
public string Name
=> nameof(ContainerWithServiceProviderFallback);
/// <inheritdoc />
public void RegisterComponent<TComponent>()
where TComponent : class
=> this.container.RegisterComponent<TComponent>();
/// <inheritdoc />
public void RegisterComponent<TComponent>(string name)
where TComponent : class
=> this.container.RegisterComponent<TComponent>(name);
/// <inheritdoc />
public void RegisterComponent<TComponent>(string name, ComponentLifeStyleType lifestyle)
where TComponent : class
=> this.container.RegisterComponent<TComponent>(name, lifestyle);
/// <inheritdoc />
public void RegisterComponent<TContract, TComponent>()
where TComponent : class
=> this.container.RegisterComponent<TContract, TComponent>();
/// <inheritdoc />
public void RegisterComponent<TContract, TComponent>(string name)
where TComponent : class
=> this.container.RegisterComponent<TContract, TComponent>(name);
/// <inheritdoc />
public void RegisterComponent<TContract, TComponent>(string name, ComponentLifeStyleType lifestyle)
where TComponent : class
=> this.container.RegisterComponent<TContract, TComponent>(name, lifestyle);
/// <inheritdoc />
public void RegisterComponent(Type componentType)
=> this.container.RegisterComponent(componentType);
/// <inheritdoc />
public void RegisterComponent(Type contractType, Type componentType)
=> this.container.RegisterComponent(contractType, componentType);
/// <inheritdoc />
public void RegisterComponent(Type contractType, Type componentType, ComponentLifeStyleType lifestyle)
=> this.container.RegisterComponent(contractType, componentType, lifestyle);
/// <inheritdoc />
public void RegisterComponent(string name, Type componentType)
=> this.container.RegisterComponent(name, componentType);
/// <inheritdoc />
public void RegisterComponent(string name, Type contractType, Type componentType)
=> this.container.RegisterComponent(name, contractType, componentType);
/// <inheritdoc />
public void RegisterComponentInstance<TContract>(string name, object instance)
=> this.container.RegisterComponentInstance<TContract>(name, instance);
/// <inheritdoc />
public void RegisterComponentSettings(string name, IDictionary dependencies)
=> this.container.RegisterComponentSettings(name, dependencies);
/// <inheritdoc />
public void RegisterComponentSettings(Type component, IDictionary dependencies)
=> this.container.RegisterComponentSettings(component, dependencies);
/// <inheritdoc />
public void RegisterComponentSettings<TComponent>(IDictionary dependencies)
=> this.container.RegisterComponentSettings<TComponent>(dependencies);
/// <inheritdoc />
public void RegisterComponent(string name, Type contractType, Type componentType, ComponentLifeStyleType lifestyle)
=> this.container.RegisterComponent(name, contractType, componentType, lifestyle);
/// <inheritdoc />
public void RegisterComponentInstance(string name, object instance)
=> this.container.RegisterComponentInstance(name, instance);
/// <inheritdoc />
public void RegisterComponentInstance(string name, Type contractType, object instance)
=> this.container.RegisterComponentInstance(name, contractType, instance);
/// <inheritdoc />
public void RegisterComponentInstance<TContract>(object instance)
=> this.container.RegisterComponentInstance<TContract>(instance);
/// <inheritdoc />
public object GetComponent(string name)
=> this.container.GetComponent(name);
/// <inheritdoc />
public TContract GetComponent<TContract>()
{
Logger.Trace($"Getting component for {typeof(TContract).FullName}");
var service = this.container.GetComponent<TContract>();
if (service is not null)
{
Logger.Trace($"Got component for {typeof(TContract).FullName} from container");
return service;
}
Logger.Trace($"Getting component for {typeof(TContract).FullName} from service provider");
return this.serviceProvider.GetService<TContract>();
}
/// <inheritdoc />
public TContract GetComponent<TContract>(string name)
=> this.container.GetComponent<TContract>(name);
/// <inheritdoc />
public object GetComponent(string name, Type contractType)
=> this.container.GetComponent(name, contractType);
/// <inheritdoc />
public string[] GetComponentList<TContract>()
=> this.container.GetComponentList<TContract>();
/// <inheritdoc />
public string[] GetComponentList(Type contractType)
=> this.container.GetComponentList(contractType);
/// <inheritdoc />
public IDictionary GetComponentSettings(string name)
=> this.container.GetComponentSettings(name);
/// <inheritdoc />
public IDictionary GetComponentSettings(Type component)
=> this.container.GetComponentSettings(component);
/// <inheritdoc />
public IDictionary GetComponentSettings<TComponent>()
=> this.container.GetComponentSettings<TComponent>();
/// <inheritdoc />
public object GetComponent(Type contractType)
=> this.container.GetComponent(contractType) ?? this.serviceProvider.GetService(contractType);
}