-
Notifications
You must be signed in to change notification settings - Fork 2
/
DefaultBootstrapper.cs
298 lines (274 loc) · 12.4 KB
/
DefaultBootstrapper.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using Canister.BaseClasses;
using Canister.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Canister.Default
{
/// <summary>
/// Default bootstrapper
/// </summary>
/// <seealso cref="BootstrapperBase{IServiceCollection}"/>
/// <seealso cref="IScope"/>
public class DefaultBootstrapper : BootstrapperBase<IServiceCollection>, IScope
{
/// <summary>
/// Initializes a new instance of the <see cref="DefaultBootstrapper"/> class.
/// </summary>
/// <param name="assemblies">The assemblies.</param>
/// <param name="collection">The collection.</param>
public DefaultBootstrapper(IEnumerable<Assembly> assemblies, IEnumerable<ServiceDescriptor> collection)
: base(assemblies)
{
AppContainer = (IServiceCollection)collection;
Register(this);
Register<IBootstrapper>(this);
ServiceProviderFactory = new DefaultServiceProviderFactory();
}
/// <summary>
/// Gets the available types.
/// </summary>
/// <value>The available types.</value>
private Type[]? AvailableTypes;
/// <summary>
/// The IoC container
/// </summary>
public override IServiceCollection AppContainer { get; }
/// <summary>
/// Name of the bootstrapper
/// </summary>
public override string Name { get; } = "Default bootstrapper";
/// <summary>
/// The <see cref="T:System.IServiceProvider"/> used to resolve dependencies from the scope.
/// </summary>
public IServiceProvider? ServiceProvider { get; private set; }
/// <summary>
/// The service provider factory
/// </summary>
private DefaultServiceProviderFactory ServiceProviderFactory { get; }
/// <summary>
/// Creates the service scope.
/// </summary>
/// <returns>The service scope</returns>
public IServiceScope CreateScope() => ServiceProvider.CreateScope();
/// <summary>
/// Registers an object with the bootstrapper
/// </summary>
/// <typeparam name="T">Object type</typeparam>
/// <param name="objectToRegister">The object to register.</param>
/// <param name="lifeTime">The life time.</param>
/// <param name="name">The name.</param>
/// <returns>This</returns>
public override IBootstrapper Register<T>(T objectToRegister, ServiceLifetime lifeTime = ServiceLifetime.Singleton, string name = "")
{
if (lifeTime == ServiceLifetime.Scoped)
AppContainer.AddScoped(_ => objectToRegister);
else if (lifeTime == ServiceLifetime.Singleton)
AppContainer.AddSingleton(_ => objectToRegister);
else
AppContainer.AddTransient(_ => objectToRegister);
if (AvailableTypes is null)
UpdateServiceProvider();
return this;
}
/// <summary>
/// Registers a type with the default constructor
/// </summary>
/// <typeparam name="T">Object type to register</typeparam>
/// <param name="lifeTime">The life time.</param>
/// <param name="name">The name.</param>
/// <returns>This</returns>
public override IBootstrapper Register<T>(ServiceLifetime lifeTime = ServiceLifetime.Transient, string name = "")
{
if (lifeTime == ServiceLifetime.Scoped)
AppContainer.AddScoped<T>();
else if (lifeTime == ServiceLifetime.Singleton)
AppContainer.AddSingleton<T>();
else
AppContainer.AddTransient<T>();
if (AvailableTypes is null)
UpdateServiceProvider();
return this;
}
/// <summary>
/// Registers a type with the default constructor of a child class
/// </summary>
/// <typeparam name="T1">Base class/interface type</typeparam>
/// <typeparam name="T2">Child class type</typeparam>
/// <param name="lifeTime">The life time.</param>
/// <param name="name">The name.</param>
/// <returns>This</returns>
public override IBootstrapper Register<T1, T2>(ServiceLifetime lifeTime = ServiceLifetime.Transient, string name = "")
{
if (lifeTime == ServiceLifetime.Scoped)
AppContainer.AddScoped<T1, T2>();
else if (lifeTime == ServiceLifetime.Singleton)
AppContainer.AddSingleton<T1, T2>();
else
AppContainer.AddTransient<T1, T2>();
if (AvailableTypes is null)
UpdateServiceProvider();
return this;
}
/// <summary>
/// Registers a type with a function
/// </summary>
/// <typeparam name="T">Type that the function returns</typeparam>
/// <param name="function">The function.</param>
/// <param name="lifeTime">The life time.</param>
/// <param name="name">The name.</param>
/// <returns>This</returns>
public override IBootstrapper Register<T>(Func<IServiceProvider, object> function, ServiceLifetime lifeTime = ServiceLifetime.Transient, string name = "")
{
if (lifeTime == ServiceLifetime.Scoped)
AppContainer.AddScoped(function);
else if (lifeTime == ServiceLifetime.Singleton)
AppContainer.AddSingleton(function);
else
AppContainer.AddTransient(function);
if (AvailableTypes is null)
UpdateServiceProvider();
return this;
}
/// <summary>
/// Registers a generic type with the default constructor
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <param name="lifeTime">The life time.</param>
/// <param name="name">The name.</param>
/// <returns>This</returns>
public override IBootstrapper Register(Type objectType, ServiceLifetime lifeTime = ServiceLifetime.Transient, string name = "")
{
if (lifeTime == ServiceLifetime.Scoped)
AppContainer.AddScoped(objectType);
else if (lifeTime == ServiceLifetime.Singleton)
AppContainer.AddSingleton(objectType);
else
AppContainer.AddTransient(objectType);
if (AvailableTypes is null)
UpdateServiceProvider();
return this;
}
/// <summary>
/// Registers all objects of a certain type with the bootstrapper
/// </summary>
/// <typeparam name="T">Object type</typeparam>
/// <param name="lifeTime">The life time.</param>
/// <returns>This</returns>
public override IBootstrapper RegisterAll<T>(ServiceLifetime lifeTime = ServiceLifetime.Transient)
{
var RegisterType = typeof(T);
foreach (var TempType in GetAvailableTypes().Where(x => IsOfType(x, typeof(T))))
{
if (lifeTime == ServiceLifetime.Scoped)
{
AppContainer.AddScoped(TempType, TempType);
AppContainer.AddScoped(RegisterType, TempType);
}
else if (lifeTime == ServiceLifetime.Singleton)
{
AppContainer.AddSingleton(TempType, TempType);
AppContainer.AddSingleton(RegisterType, TempType);
}
else
{
AppContainer.AddTransient(TempType, TempType);
AppContainer.AddTransient(RegisterType, TempType);
}
}
if (AvailableTypes is null)
UpdateServiceProvider();
return this;
}
/// <summary>
/// Resolves the object based on the type specified
/// </summary>
/// <typeparam name="T">Type to resolve</typeparam>
/// <param name="defaultObject">The default object.</param>
/// <returns>An object of the specified type</returns>
public override T Resolve<T>(T? defaultObject = null)
where T : class => ServiceProvider.GetRequiredService<T>();
/// <summary>
/// Resolves the object based on the type specified
/// </summary>
/// <typeparam name="T">Type to resolve</typeparam>
/// <param name="name">The name.</param>
/// <param name="defaultObject">The default object.</param>
/// <returns>An object of the specified type</returns>
public override T Resolve<T>(string name, T? defaultObject = null)
where T : class => ServiceProvider.GetRequiredService<T>();
/// <summary>
/// Resolves the object based on the type specified
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <param name="defaultObject">The default object.</param>
/// <returns>An object of the specified type</returns>
public override object Resolve(Type objectType, object? defaultObject = null) => ServiceProvider.GetRequiredService(objectType);
/// <summary>
/// Resolves the object based on the type specified
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <param name="name">The name.</param>
/// <param name="defaultObject">The default object.</param>
/// <returns>An object of the specified type</returns>
public override object Resolve(Type objectType, string name, object? defaultObject = null) => ServiceProvider.GetRequiredService(objectType);
/// <summary>
/// Resolves the objects based on the type specified
/// </summary>
/// <typeparam name="T">Type to resolve</typeparam>
/// <returns>A list of objects of the specified type</returns>
public override IEnumerable<T> ResolveAll<T>() => ServiceProvider.GetRequiredService<IEnumerable<T>>();
/// <summary>
/// Resolves all objects based on the type specified
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <returns>A list of objects of the specified type</returns>
public override IEnumerable<object> ResolveAll(Type objectType) => ServiceProvider.GetRequiredService(typeof(IEnumerable<>).MakeGenericType(objectType)) as IEnumerable<object> ?? Array.Empty<object>();
/// <summary>
/// Called after the build step.
/// </summary>
protected override void AfterBuild()
{
AvailableTypes = null;
ServiceProvider = new DefaultServiceProviderFactory().CreateServiceProvider(AppContainer);
}
/// <summary>
/// Called before the build step.
/// </summary>
protected override void BeforeBuild() => AvailableTypes = GetAvailableTypes();
/// <summary>
/// Runs before the module resolve.
/// </summary>
protected override void BeforeModuleResolve() => ServiceProvider = new DefaultServiceProviderFactory().CreateServiceProvider(AppContainer);
/// <summary>
/// Gets the available types.
/// </summary>
/// <returns>The available types.</returns>
private Type[] GetAvailableTypes()
{
return AvailableTypes
?? Assemblies.SelectMany(x =>
{
try
{
return x.GetTypes();
}
catch (ReflectionTypeLoadException) { return Array.Empty<Type>(); }
}).Where(x => x.IsClass
&& !x.IsAbstract
&& !x.ContainsGenericParameters)
.ToArray();
}
/// <summary>
/// Updates the service provider.
/// </summary>
private void UpdateServiceProvider()
{
if (ServiceProvider is null)
return;
ServiceProvider = ServiceProviderFactory.CreateServiceProvider(AppContainer);
}
}
}