-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathModuleBase.cs
More file actions
58 lines (51 loc) · 1.77 KB
/
ModuleBase.cs
File metadata and controls
58 lines (51 loc) · 1.77 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
using System;
using System.Threading.Tasks;
using Ultz.Extensions.Commands.Built;
using Ultz.Extensions.Commands.Context;
namespace Ultz.Extensions.Commands.ModuleBases
{
/// <summary>
/// Makes the inheriting class a <see cref="Module" /> that can be added to the <see cref="CommandService" />.
/// </summary>
/// <typeparam name="TContext"> The <see cref="CommandContext" /> this <see cref="Module" /> will use. </typeparam>
public abstract class ModuleBase<TContext> : IModuleBase
where TContext : CommandContext
{
/// <summary>
/// The execution context.
/// </summary>
protected TContext Context { get; private set; }
ValueTask IModuleBase.BeforeExecutedAsync()
{
return BeforeExecutedAsync();
}
ValueTask IModuleBase.AfterExecutedAsync()
{
return AfterExecutedAsync();
}
void IModuleBase.Prepare(CommandContext context)
{
Prepare(context);
}
/// <summary>
/// Fires before a <see cref="Command" /> in this <see cref="Module" /> is executed.
/// </summary>
protected virtual ValueTask BeforeExecutedAsync()
{
return default;
}
/// <summary>
/// Fires after a <see cref="Command" /> in this <see cref="Module" /> is executed.
/// </summary>
protected virtual ValueTask AfterExecutedAsync()
{
return default;
}
internal void Prepare(CommandContext context)
{
Context = context as TContext ??
throw new InvalidOperationException(
$"Unable to set the context. Expected {typeof(TContext)}, got {context.GetType()}.");
}
}
}