-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkspaceServiceCollectionExtensions.cs
More file actions
74 lines (62 loc) · 3.95 KB
/
Copy pathWorkspaceServiceCollectionExtensions.cs
File metadata and controls
74 lines (62 loc) · 3.95 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
using Microsoft.Extensions.DependencyInjection;
using RustPlusBot.Discord;
using RustPlusBot.Features.Workspace.Gateway;
using RustPlusBot.Features.Workspace.Locating;
using RustPlusBot.Features.Workspace.Messages;
using RustPlusBot.Features.Workspace.Reconciler;
using RustPlusBot.Features.Workspace.Registry;
using RustPlusBot.Features.Workspace.Specs;
using RustPlusBot.Features.Workspace.Teardown;
using RustPlusBot.Localization;
namespace RustPlusBot.Features.Workspace;
/// <summary>DI registration for the workspace provisioning feature.</summary>
public static class WorkspaceServiceCollectionExtensions
{
/// <summary>Registers the registry, gateway, reconciler, renderers, teardown, and module seam.</summary>
/// <param name="services">The service collection to add to.</param>
/// <returns>The same service collection, for chaining.</returns>
public static IServiceCollection AddWorkspace(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);
// Spec registry (stateless singletons).
services.AddSingleton<IChannelSpecProvider, GlobalWorkspaceSpecProvider>();
services.AddSingleton<IMessageSpecProvider, GlobalWorkspaceSpecProvider>();
services.AddSingleton<IChannelSpecProvider, ServerWorkspaceSpecProvider>();
services.AddSingleton<IMessageSpecProvider, ServerWorkspaceSpecProvider>();
services.AddSingleton<IWorkspaceRegistry, WorkspaceRegistry>();
// Localization.
services.AddRustPlusBotLocalization();
// Gateway + per-guild lock.
services.AddSingleton<IWorkspaceGateway, DiscordWorkspaceGateway>();
services.AddSingleton<IProvisioningLock, ProvisioningLock>();
// Renderers (scoped: some query the DbContext via IServerService).
services.AddScoped<IMessageRenderer, InformationMessageRenderer>();
services.AddScoped<IMessageRenderer, SetupMessageRenderer>();
services.AddScoped<IMessageRenderer, SettingsMessageRenderer>();
// ServerInfoMessageRenderer requires IRustServerQuery, which is registered by AddConnections —
// the host must compose both AddWorkspace and AddConnections.
services.AddScoped<IMessageRenderer, ServerInfoMessageRenderer>();
services.AddScoped<IMessageRenderer, MapControlMessageRenderer>();
// Reconciler + teardown (scoped). Register the teardown service once and expose both interfaces
// off the same scoped instance, so resolving either does not create a second instance.
services.AddScoped<WorkspaceBackends>();
services.AddScoped<IWorkspaceReconciler, WorkspaceReconciler>();
services.AddScoped<WorkspaceTeardownService>();
services.AddScoped<IWorkspaceTeardownService>(sp => sp.GetRequiredService<WorkspaceTeardownService>());
services.AddScoped<IServerWorkspaceRemover>(sp => sp.GetRequiredService<WorkspaceTeardownService>());
services.AddScoped<IGuildPurgeService, GuildPurgeService>();
// Options (Host binds the "Workspace" section; default = danger commands off).
services.AddOptions<WorkspaceOptions>();
// Contribute this assembly's interaction modules to the Discord layer.
services.AddSingleton(new InteractionModuleAssembly(typeof(WorkspaceServiceCollectionExtensions).Assembly));
// Channel locators (singleton with TTL cache; IClock + IServiceScopeFactory provided by the host).
services.AddSingleton<ITeamChatChannelLocator, TeamChatChannelLocator>();
services.AddSingleton<IEventChannelLocator, EventChannelLocator>();
services.AddSingleton<IMapChannelLocator, MapChannelLocator>();
services.AddSingleton<ISwitchChannelLocator, SwitchChannelLocator>();
services.AddSingleton<IAlarmChannelLocator, AlarmChannelLocator>();
services.AddSingleton<IStorageMonitorChannelLocator, StorageMonitorChannelLocator>();
services.AddHostedService<Hosting.WorkspaceHostedService>();
return services;
}
}