/
ServiceContainer.cs
264 lines (230 loc) · 9.53 KB
/
ServiceContainer.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics;
namespace System.ComponentModel.Design
{
/// <summary>
/// This is a simple implementation of IServiceContainer.
/// </summary>
public class ServiceContainer : IServiceContainer, IDisposable
{
private ServiceCollection<object?>? _services;
private readonly IServiceProvider? _parentProvider;
private static readonly Type[] s_defaultServices = new Type[] { typeof(IServiceContainer), typeof(ServiceContainer) };
/// <summary>
/// Creates a new service object container.
/// </summary>
public ServiceContainer()
{
}
/// <summary>
/// Creates a new service object container.
/// </summary>
public ServiceContainer(IServiceProvider? parentProvider)
{
_parentProvider = parentProvider;
}
/// <summary>
/// Retrieves the parent service container, or null if there is no parent container.
/// </summary>
private IServiceContainer? Container
{
get => _parentProvider?.GetService(typeof(IServiceContainer)) as IServiceContainer;
}
/// <summary>
/// This property returns the default services that are implemented directly on this IServiceContainer.
/// the default implementation of this property is to return the IServiceContainer and ServiceContainer
/// types. You may override this property and return your own types, modifying the default behavior
/// of GetService.
/// </summary>
protected virtual Type[] DefaultServices => s_defaultServices;
/// <summary>
/// Our collection of services. The service collection is demand
/// created here.
/// </summary>
private ServiceCollection<object?> Services => _services ??= new ServiceCollection<object?>();
/// <summary>
/// Adds the given service to the service container.
/// </summary>
public void AddService(Type serviceType, object serviceInstance)
{
AddService(serviceType, serviceInstance, false);
}
/// <summary>
/// Adds the given service to the service container.
/// </summary>
public virtual void AddService(Type serviceType, object serviceInstance, bool promote)
{
if (promote)
{
IServiceContainer? container = Container;
if (container != null)
{
container.AddService(serviceType, serviceInstance, promote);
return;
}
}
// We're going to add this locally. Ensure that the service instance
// is correct.
//
ArgumentNullException.ThrowIfNull(serviceType);
ArgumentNullException.ThrowIfNull(serviceInstance);
if (!(serviceInstance is ServiceCreatorCallback) && !serviceInstance.GetType().IsCOMObject && !serviceType.IsInstanceOfType(serviceInstance))
{
throw new ArgumentException(SR.Format(SR.ErrorInvalidServiceInstance, serviceType.FullName));
}
if (Services.ContainsKey(serviceType))
{
throw new ArgumentException(SR.Format(SR.ErrorServiceExists, serviceType.FullName), nameof(serviceType));
}
Services[serviceType] = serviceInstance;
}
/// <summary>
/// Adds the given service to the service container.
/// </summary>
public void AddService(Type serviceType, ServiceCreatorCallback callback)
{
AddService(serviceType, callback, false);
}
/// <summary>
/// Adds the given service to the service container.
/// </summary>
public virtual void AddService(Type serviceType, ServiceCreatorCallback callback, bool promote)
{
if (promote)
{
IServiceContainer? container = Container;
if (container != null)
{
container.AddService(serviceType, callback, promote);
return;
}
}
// We're going to add this locally. Ensure that the service instance
// is correct.
//
ArgumentNullException.ThrowIfNull(serviceType);
ArgumentNullException.ThrowIfNull(callback);
if (Services.ContainsKey(serviceType))
{
throw new ArgumentException(SR.Format(SR.ErrorServiceExists, serviceType.FullName), nameof(serviceType));
}
Services[serviceType] = callback;
}
/// <summary>
/// Disposes this service container. This also walks all instantiated services within the container
/// and disposes any that implement IDisposable, and clears the service list.
/// </summary>
public void Dispose()
{
Dispose(true);
}
/// <summary>
/// Disposes this service container. This also walks all instantiated services within the container
/// and disposes any that implement IDisposable, and clears the service list.
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
ServiceCollection<object?>? serviceCollection = _services;
_services = null;
if (serviceCollection != null)
{
foreach (object? o in serviceCollection.Values)
{
if (o is IDisposable)
{
((IDisposable)o).Dispose();
}
}
}
}
}
/// <summary>
/// Retrieves the requested service.
/// </summary>
public virtual object? GetService(Type serviceType)
{
object? service = null;
// Try locally. We first test for services we
// implement and then look in our service collection.
Type[] defaults = DefaultServices;
for (int idx = 0; idx < defaults.Length; idx++)
{
if (serviceType != null && serviceType.IsEquivalentTo(defaults[idx]))
{
service = this;
break;
}
}
if (service == null && serviceType != null)
{
Services.TryGetValue(serviceType, out service);
}
// Is the service a creator delegate?
if (service is ServiceCreatorCallback)
{
service = ((ServiceCreatorCallback)service)(this, serviceType!);
if (service != null && !service.GetType().IsCOMObject && !serviceType!.IsInstanceOfType(service))
{
// Callback passed us a bad service. NULL it, rather than throwing an exception.
// Callers here do not need to be prepared to handle bad callback implementations.
service = null;
}
// And replace the callback with our new service.
Services[serviceType!] = service;
}
if (service == null && _parentProvider != null)
{
service = _parentProvider.GetService(serviceType!);
}
return service;
}
/// <summary>
/// Removes the given service type from the service container.
/// </summary>
public void RemoveService(Type serviceType)
{
RemoveService(serviceType, false);
}
/// <summary>
/// Removes the given service type from the service container.
/// </summary>
public virtual void RemoveService(Type serviceType, bool promote)
{
if (promote)
{
IServiceContainer? container = Container;
if (container != null)
{
container.RemoveService(serviceType, promote);
return;
}
}
// We're going to remove this from our local list.
ArgumentNullException.ThrowIfNull(serviceType);
Services.Remove(serviceType);
}
/// <summary>
/// Use this collection to store mapping from the Type of a service to the object that provides it in a way
/// that is aware of embedded types. The comparer for this collection will call Type.IsEquivalentTo(...)
/// instead of doing a reference comparison which will fail in type embedding scenarios. To speed the lookup
/// performance we will use hash code of Type.FullName.
/// </summary>
/// <typeparam name="T"></typeparam>
private sealed class ServiceCollection<T> : Dictionary<Type, T>
{
private static readonly EmbeddedTypeAwareTypeComparer s_serviceTypeComparer = new EmbeddedTypeAwareTypeComparer();
private sealed class EmbeddedTypeAwareTypeComparer : IEqualityComparer<Type>
{
public bool Equals(Type? x, Type? y) => x!.IsEquivalentTo(y);
public int GetHashCode(Type obj) => obj.FullName!.GetHashCode();
}
public ServiceCollection() : base(s_serviceTypeComparer)
{
}
}
}
}