-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.cs
More file actions
88 lines (74 loc) · 2.59 KB
/
Module.cs
File metadata and controls
88 lines (74 loc) · 2.59 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
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Modulight.Modules.Hosting;
namespace Modulight.Modules
{
/// <summary>
/// Specifies the contract for modules.
/// </summary>
public interface IModule
{
/// <summary>
/// Initialize the module.
/// </summary>
/// <returns></returns>
Task Initialize(CancellationToken cancellationToken = default);
/// <summary>
/// Shutdown the module.
/// </summary>
/// <returns></returns>
Task Shutdown(CancellationToken cancellationToken = default);
/// <summary>
/// Get the module manifest.
/// </summary>
ModuleManifest Manifest { get; }
}
/// <summary>
/// Basic implementation for <see cref="IModule"/>, cooperated with <see cref="IModuleHost"/>.
/// </summary>
public abstract class Module : IModule
{
readonly Lazy<ModuleManifest> _manifest;
/// <summary>
/// Get the logger.
/// </summary>
protected ILogger Logger { get; }
/// <summary>
/// Get the module host.
/// </summary>
protected IModuleHost Host { get; }
/// <summary>
/// Get the service provider.
/// </summary>
protected IServiceProvider Services { get; }
/// <summary>
/// Create module instance.
/// </summary>
/// <param name="host"></param>
protected Module(IModuleHost host)
{
Host = host;
Services = host.Services;
Logger = Services.GetRequiredService<ILogger<Module>>();
_manifest = new Lazy<ModuleManifest>(() => Host.GetManifest(GetType()));
}
/// <inheritdoc/>
public ModuleManifest Manifest => _manifest.Value;
/// <inheritdoc/>
protected T GetService<T>(IServiceProvider provider) where T : notnull => Host.GetService<T>(provider, GetType());
/// <inheritdoc/>
protected T GetOption<T>(IServiceProvider provider) where T : class => Host.GetOption<T>(provider, GetType());
/// <inheritdoc/>
public virtual Task Initialize(CancellationToken cancellationToken = default)
{
Logger.LogDebug($"Module Initialized: {Manifest.FullName}.");
return Task.CompletedTask;
}
/// <inheritdoc/>
public virtual Task Shutdown(CancellationToken cancellationToken = default)
{
Logger.LogDebug($"Module Shutdowned: {Manifest.FullName}.");
return Task.CompletedTask;
}
}
}