/
CommandsServiceBuilder.cs
403 lines (361 loc) · 23 KB
/
CommandsServiceBuilder.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
using System;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using TehGM.Wolfringo.Commands.Initialization;
using TehGM.Wolfringo.Commands.Parsing;
using TehGM.Wolfringo.Utilities.Internal;
namespace TehGM.Wolfringo.Commands
{
/// <summary>A builder for <see cref="CommandsService"/>.</summary>
public class CommandsServiceBuilder
{
/// <summary>Options for the client.</summary>
public CommandsOptions Options { get; set; }
private readonly IServiceCollection _services;
private readonly DisposableServicesHandler _disposablesHandler;
private CancellationToken _cancellationToken;
/// <summary>Invoked when the builder is about to build a new instance of <see cref="CommandsService"/>.</summary>
public event Action<IServiceCollection> Building;
/// <summary>Invoked when the builder has finished building a new instance of <see cref="CommandsService"/>.</summary>
public event Action<CommandsService, IServiceProvider> Built;
/// <summary>Creates a new CommandsService builder with provided services.</summary>
/// <param name="services">Initial services to use.</param>
public CommandsServiceBuilder(IServiceCollection services)
{
this.Options = new CommandsOptions();
this._services = services;
this._disposablesHandler = new DisposableServicesHandler();
this._cancellationToken = default;
// initialize defaults
if (!this._services.HasService<IArgumentsParser>())
this.WithDefaultArgumentsParser();
if (!this._services.HasService<IArgumentConverterProvider>())
this.WithDefaultArgumentsConverterProvider();
if (!this._services.HasService<ICommandInitializerProvider>())
this.WithDefaultCommandInitializerProvider();
if (!this._services.HasService<ICommandsHandlerProvider>())
this.WithDefaultCommandsHandlerProvider();
if (!this._services.HasService<ICommandsLoader>())
this.WithDefaultCommandsLoader();
if (!this._services.HasService<IParameterBuilder>())
this.WithDefaultParameterBuilder();
}
/// <summary>Creates a new WolfClient builder with default values pre-set.</summary>
public CommandsServiceBuilder()
: this(new ServiceCollection()) { }
#region CUSTOM SERVICES
// SINGLETON
/// <summary>Adds any custom service so it can be resolved during Dependency Injection.</summary>
/// <typeparam name="TService">Type of the service.</typeparam>
/// <param name="service">The service instance.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithSingletonService<TService>(TService service) where TService : class
{
if (service == null)
throw new ArgumentNullException(nameof(service));
this._disposablesHandler.UnmarkForDisposal<TService>();
this._services.RemoveService<TService>();
this._services.AddSingleton<TService>(service);
return this;
}
/// <summary>Adds any custom service so it can be resolved during Dependency Injection.</summary>
/// <typeparam name="TService">Type of the service.</typeparam>
/// <param name="factory">Delegate that will be invoked when the service is resolved.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithSingletonService<TService>(Func<IServiceProvider, TService> factory) where TService : class
{
this._disposablesHandler.MarkForDisposal<TService>();
this._services.RemoveService<TService>();
this._services.AddSingleton<TService>(factory);
return this;
}
/// <summary>Adds any custom service so it can be resolved during Dependency Injection.</summary>
/// <typeparam name="TService">Type of the service.</typeparam>
/// <typeparam name="TImplementation">Implementation type of the service</typeparam>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithSingletonService<TService, TImplementation>() where TService : class where TImplementation : class, TService
{
this._disposablesHandler.MarkForDisposal<TService>();
this._services.RemoveService<TService>();
this._services.AddSingleton<TService, TImplementation>();
return this;
}
// SCOPED
/// <summary>Adds any custom service so it can be resolved during Dependency Injection.</summary>
/// <typeparam name="TService">Type of the service.</typeparam>
/// <param name="factory">Delegate that will be invoked when the service is resolved.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithScopedService<TService>(Func<IServiceProvider, TService> factory) where TService : class
{
this._disposablesHandler.UnmarkForDisposal<TService>();
this._services.RemoveService<TService>();
this._services.AddScoped<TService>(factory);
return this;
}
/// <summary>Adds any custom service so it can be resolved during Dependency Injection.</summary>
/// <typeparam name="TService">Type of the service.</typeparam>
/// <typeparam name="TImplementation">Implementation type of the service</typeparam>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithScopedService<TService, TImplementation>() where TService : class where TImplementation : class, TService
{
this._disposablesHandler.UnmarkForDisposal<TService>();
this._services.RemoveService<TService>();
this._services.AddScoped<TService, TImplementation>();
return this;
}
// TRANSIENT
/// <summary>Adds any custom service so it can be resolved during Dependency Injection.</summary>
/// <typeparam name="TService">Type of the service.</typeparam>
/// <param name="factory">Delegate that will be invoked when the service is resolved.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithTransientService<TService>(Func<IServiceProvider, TService> factory) where TService : class
{
this._disposablesHandler.UnmarkForDisposal<TService>();
this._services.RemoveService<TService>();
this._services.AddTransient<TService>(factory);
return this;
}
/// <summary>Adds any custom service so it can be resolved during Dependency Injection.</summary>
/// <typeparam name="TService">Type of the service.</typeparam>
/// <typeparam name="TImplementation">Implementation type of the service</typeparam>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithTransientService<TService, TImplementation>() where TService : class where TImplementation : class, TService
{
this._disposablesHandler.UnmarkForDisposal<TService>();
this._services.RemoveService<TService>();
this._services.AddTransient<TService, TImplementation>();
return this;
}
#endregion
#region WOLF CLIENT
/// <summary>Sets WOLF client to use with the <see cref="CommandsService"/>.</summary>
/// <param name="client">The WOLF client.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithWolfClient(IWolfClient client)
=> this.WithSingletonService<IWolfClient>(client);
/// <summary>Sets WOLF client to use with the <see cref="CommandsService"/>.</summary>
/// <typeparam name="TImplementation">Implementation type of the WOLF client.</typeparam>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithWolfClient<TImplementation>() where TImplementation : class, IWolfClient
=> this.WithSingletonService<IWolfClient, TImplementation>();
/// <summary>Sets WOLF client to use with the <see cref="CommandsService"/>.</summary>
/// <remarks>This method allows you configure the client using <see cref="WolfClientBuilder"/>.</remarks>
/// <param name="clientBuilder">A delegate that allows configuring the <see cref="WolfClient"/> before creation.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithWolfClient(Action<WolfClientBuilder> clientBuilder)
{
WolfClientBuilder builder = new WolfClientBuilder(this._services);
clientBuilder?.Invoke(builder);
return this.WithSingletonService<IWolfClient>(_ => builder.Build());
}
#endregion
#region OPTIONS
/// <summary>Sets the default prefix for commands.</summary>
/// <param name="prefix">Prefix value.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithPrefix(string prefix)
=> this.ConfigureOptions(options => options.Prefix = prefix);
/// <summary>Sets whether commands are case-sensitive by default.</summary>
/// <param name="caseSensitive">Case sensitivity setting.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithCaseSensitivity(bool caseSensitive)
=> this.ConfigureOptions(options => options.CaseSensitivity = caseSensitive);
/// <summary>Sets default prefix requirement for commands.</summary>
/// <param name="requirePrefix">Prefix requirement.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithPrefixRequirement(PrefixRequirement requirePrefix)
=> this.ConfigureOptions(options => options.RequirePrefix = requirePrefix);
/// <summary>Enables or disables default help command functionality.</summary>
/// <param name="enabled">Whether the default help command should be enabled.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithDefaultHelpCommand(bool enabled)
=> this.ConfigureOptions(options => options.EnableDefaultHelpCommand = enabled);
/// <summary>Enables default help command functionality.</summary>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithDefaultHelpCommand()
=> this.WithDefaultHelpCommand(true);
/// <summary>Allows configuring options for commands service.</summary>
/// <param name="configure">Delegate that can be used for options configuration.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder ConfigureOptions(Action<CommandsOptions> configure)
{
configure.Invoke(this.Options);
return this;
}
#endregion
#region ARGUMENTS PARSER
/// <summary>Sets arguments parser.</summary>
/// <param name="parser">The arguments parser.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithArgumentsParser(IArgumentsParser parser)
=> this.WithSingletonService<IArgumentsParser>(parser);
/// <summary>Sets arguments parser.</summary>
/// <typeparam name="TImplementation">Implementation type of the arguments parser.</typeparam>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithArgumentsParser<TImplementation>() where TImplementation : class, IArgumentsParser
=> this.WithSingletonService<IArgumentsParser, TImplementation>();
/// <summary>Switches to default arguments parser.</summary>
/// <param name="options">Options for the default parser.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithDefaultArgumentsParser(ArgumentsParserOptions options)
=> this.WithSingletonService<IArgumentsParser>(_ => new ArgumentsParser(options));
/// <summary>Switches to default arguments parser with default options.</summary>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithDefaultArgumentsParser()
=> this.WithArgumentsParser<ArgumentsParser>();
#endregion
#region ARGUMENTS CONVERTER PROVIDER
/// <summary>Sets argument converter provider.</summary>
/// <param name="provider">The argument converter provider.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithArgumentsConverterProvider(IArgumentConverterProvider provider)
=> this.WithSingletonService<IArgumentConverterProvider>(provider);
/// <summary>Sets argument converter provider.</summary>
/// <typeparam name="TImplementation">Implementation type of argument converter provider.</typeparam>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithArgumentsConverterProvider<TImplementation>() where TImplementation : class, IArgumentConverterProvider
=> this.WithSingletonService<IArgumentConverterProvider, TImplementation>();
/// <summary>Switches to default argument converter provider.</summary>
/// <param name="options">Options for the argument converter provider.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithDefaultArgumentsConverterProvider(ArgumentConverterProviderOptions options)
=> this.WithSingletonService<IArgumentConverterProvider>(_ => new ArgumentConverterProvider(options));
/// <summary>Switches to default argument converter provider with default options.</summary>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithDefaultArgumentsConverterProvider()
=> this.WithArgumentsConverterProvider<ArgumentConverterProvider>();
#endregion
#region COMMANDS HANDLER PROVIDER
/// <summary>Sets commands handler provider.</summary>
/// <param name="provider">The commands handler provider.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithCommandsHandlerProvider(ICommandsHandlerProvider provider)
=> this.WithSingletonService<ICommandsHandlerProvider>(provider);
/// <summary>Sets commands handler provider.</summary>
/// <typeparam name="TImplementation">Implementation type of commands handler provider.</typeparam>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithCommandsHandlerProvider<TImplementation>() where TImplementation : class, ICommandsHandlerProvider
=> this.WithSingletonService<ICommandsHandlerProvider, TImplementation>();
/// <summary>Switches to default commands handler provider.</summary>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithDefaultCommandsHandlerProvider()
=> this.WithCommandsHandlerProvider<CommandsHandlerProvider>();
#endregion
#region PARAMETER BUILDER
/// <summary>Sets parameter builder.</summary>
/// <param name="builder">The parameter builder.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithParameterBuilder(IParameterBuilder builder)
=> this.WithSingletonService<IParameterBuilder>(builder);
/// <summary>Sets parameter builder.</summary>
/// <typeparam name="TImplementation">Implementation type of the parameter builder.</typeparam>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithParameterBuilder<TImplementation>() where TImplementation : class, IParameterBuilder
=> this.WithSingletonService<IParameterBuilder, TImplementation>();
/// <summary>Switches to default parameter builder.</summary>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithDefaultParameterBuilder()
=> this.WithParameterBuilder<ParameterBuilder>();
#endregion
#region COMMAND INITIALIZER PROVIDER
/// <summary>Sets command initializer provider.</summary>
/// <param name="provider">The command initializer provider.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithCommandInitializerProvider(ICommandInitializerProvider provider)
=> this.WithSingletonService<ICommandInitializerProvider>(provider);
/// <summary>Sets command initializer provider.</summary>
/// <typeparam name="TImplementation">Implementation type of the command initializer provider.</typeparam>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithCommandInitializerProvider<TImplementation>() where TImplementation : class, ICommandInitializerProvider
=> this.WithSingletonService<ICommandInitializerProvider, TImplementation>();
/// <summary>Switches to default command initializer provider.</summary>
/// <param name="options">Options for the command initializer provider.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithDefaultCommandInitializerProvider(CommandInitializerProviderOptions options)
=> this.WithSingletonService<ICommandInitializerProvider>(_ => new CommandInitializerProvider(options));
/// <summary>Switches to default command initializer provider with default options.</summary>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithDefaultCommandInitializerProvider()
=> this.WithCommandInitializerProvider<CommandInitializerProvider>();
#endregion
#region COMMANDS LOADER
/// <summary>Sets commands loader.</summary>
/// <param name="loader">The commands loader.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithCommandsLoader(ICommandsLoader loader)
=> this.WithSingletonService<ICommandsLoader>(loader);
/// <summary>Sets commands loader.</summary>
/// <typeparam name="TImplementation">Implementation type of the commands loader.</typeparam>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithCommandsLoader<TImplementation>() where TImplementation : class, ICommandsLoader
=> this.WithSingletonService<ICommandsLoader, TImplementation>();
/// <summary>Switches to default commands loader.</summary>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithDefaultCommandsLoader()
=> this.WithSingletonService<ICommandsLoader>(provider => new CommandsLoader(provider.GetRequiredService<ICommandInitializerProvider>(),
provider.GetLoggerFor<ICommandsLoader, CommandsLoader>()));
#endregion
#region LOGGING
/// <summary>Sets a logger to be used by the <see cref="CommandsService"/>.</summary>
/// <param name="logger">Logger to use.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithLogging(ILogger logger)
{
this._services.RemoveService<ILoggerFactory>();
if (logger is ILogger<CommandsService> typedLogger)
this.WithSingletonService<ILogger<CommandsService>>(typedLogger);
if (logger is ILogger<ICommandsService> interfaceTypedLogger)
this.WithSingletonService<ILogger<ICommandsService>>(interfaceTypedLogger);
return this.WithSingletonService<ILogger>(logger);
}
/// <summary>Sets a logger factory that will be used to create a logger.</summary>
/// <param name="factory">Logger factory to use when creating a logger.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithLogging(ILoggerFactory factory)
{
this.WithSingletonService<ILogger<CommandsService>>(_ => factory.CreateLogger<CommandsService>());
this.WithSingletonService<ILogger<ICommandsService>>(_ => factory.CreateLogger<ICommandsService>());
this.WithSingletonService<ILogger>(_ => factory.CreateLogger<CommandsService>());
return this.WithSingletonService<ILoggerFactory>(factory);
}
#endregion
#region CANCELLATION TOKEN
/// <summary>Sets cancellation token that can be used for cancelling all tasks within <see cref="CommandsService"/>.</summary>
/// <param name="cancellationToken">Cancellation token to use.</param>
/// <returns>Current builder instance.</returns>
public CommandsServiceBuilder WithCancellationToken(CancellationToken cancellationToken)
{
this._cancellationToken = cancellationToken;
return this;
}
#endregion
/// <summary>Builds a new CommandsService with provided values.</summary>
/// <param name="client">The WOLF Client that is used by the built CommandsService.</param>
/// <param name="services">The built service provider.</param>
/// <returns>A new CommandsService instance.</returns>
public CommandsService Build(out IWolfClient client, out IServiceProvider services)
{
if (!this._services.HasService<IWolfClient>())
throw new InvalidOperationException($"Cannot create commands service without WOLF Client. Please use {nameof(this.WithWolfClient)} before calling {nameof(this.Build)}.");
// add options and disposables handler before building
this.WithSingletonService<CommandsOptions>(this.Options);
this.WithSingletonService<ICommandOptions>(provider => provider.GetRequiredService<CommandsOptions>());
this.WithSingletonService<DisposableServicesHandler>(this._disposablesHandler);
// register self result
this.Building?.Invoke(this._services);
this._services.AddSingleton<CommandsService>(provider => new CommandsService(provider, this.Options));
this._services.AddSingleton<ICommandsService>(provider => provider.GetRequiredService<CommandsService>());
// build and return
services = this._services.BuildServiceProvider();
client = services.GetRequiredService<IWolfClient>();
CommandsService result = services.GetRequiredService<CommandsService>();
this.Built?.Invoke(result, services);
return result;
}
/// <summary>Builds a new CommandsService with provided values.</summary>
/// <returns>A new CommandsService instance.</returns>
public CommandsService Build()
=> this.Build(out _, out _);
}
}