From 2751abc44dadada919b9f293b16e7531022bdd33 Mon Sep 17 00:00:00 2001 From: Dominion Date: Sun, 4 Jun 2023 11:57:04 -0400 Subject: [PATCH] Move GitHubService initialization to ServiceCollectionExtensions and make modifiable --- src/Tgstation.Server.Host/Core/Application.cs | 5 +-- .../Extensions/ServiceCollectionExtensions.cs | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Tgstation.Server.Host/Core/Application.cs b/src/Tgstation.Server.Host/Core/Application.cs index 99ea878a1d..cab990f0e3 100644 --- a/src/Tgstation.Server.Host/Core/Application.cs +++ b/src/Tgstation.Server.Host/Core/Application.cs @@ -51,7 +51,6 @@ using Tgstation.Server.Host.Swarm; using Tgstation.Server.Host.System; using Tgstation.Server.Host.Transfer; using Tgstation.Server.Host.Utils; -using Tgstation.Server.Host.Utils.GitHub; namespace Tgstation.Server.Host.Core { @@ -372,9 +371,7 @@ namespace Tgstation.Server.Host.Core services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(x => x.GetRequiredService().CreateService()); + services.AddGitHub(); // configure root services services.AddSingleton(); diff --git a/src/Tgstation.Server.Host/Extensions/ServiceCollectionExtensions.cs b/src/Tgstation.Server.Host/Extensions/ServiceCollectionExtensions.cs index 4980d732d8..0d8e199285 100644 --- a/src/Tgstation.Server.Host/Extensions/ServiceCollectionExtensions.cs +++ b/src/Tgstation.Server.Host/Extensions/ServiceCollectionExtensions.cs @@ -15,6 +15,7 @@ using Serilog.Sinks.Elasticsearch; using Tgstation.Server.Host.Components.Chat.Providers; using Tgstation.Server.Host.Configuration; using Tgstation.Server.Host.Utils; +using Tgstation.Server.Host.Utils.GitHub; namespace Tgstation.Server.Host.Extensions { @@ -28,6 +29,11 @@ namespace Tgstation.Server.Host.Extensions /// static Type chatProviderFactoryType = typeof(ProviderFactory); + /// + /// The implementation used in calls to . + /// + static Type gitHubServiceFactoryType = typeof(GitHubServiceFactory); + /// /// A for an additional to use. /// @@ -42,6 +48,32 @@ namespace Tgstation.Server.Host.Extensions chatProviderFactoryType = typeof(TProviderFactory); } + /// + /// Change the used as an implementation for calls to . + /// + /// The implementation to use. + public static void UseGitHubServiceFactory() where TGitHubServiceFactory : IGitHubServiceFactory + { + gitHubServiceFactoryType = typeof(TGitHubServiceFactory); + } + + /// + /// Adds a implementation to the given . + /// + /// The to configure. + /// . + public static IServiceCollection AddGitHub(this IServiceCollection serviceCollection) + { + if (serviceCollection == null) + throw new ArgumentNullException(nameof(serviceCollection)); + + serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(typeof(IGitHubServiceFactory), gitHubServiceFactoryType); + serviceCollection.AddSingleton(x => x.GetRequiredService().CreateService()); + + return serviceCollection; + } + /// /// Add an additional to s that call . /// @@ -60,6 +92,9 @@ namespace Tgstation.Server.Host.Extensions /// . public static IServiceCollection AddChatProviderFactory(this IServiceCollection serviceCollection) { + if (serviceCollection == null) + throw new ArgumentNullException(nameof(serviceCollection)); + return serviceCollection.AddSingleton(typeof(IProviderFactory), chatProviderFactoryType); }