From 25a65d230da0f4dcde34b5e17d27b9e867115e8b Mon Sep 17 00:00:00 2001 From: "Dylan R. E. Moonfire" Date: Sat, 11 Sep 2021 00:42:04 -0500 Subject: [PATCH] fix: change config and logging services to be singletons --- .../ConfigToolGlobalService.cs} | 10 +++++----- .../LoggingToolGlobalService.cs} | 6 +++--- src/MfGames.ToolBuilder/ToolBuilder.cs | 20 +++++++++++++++---- src/MfGames.ToolBuilder/ToolBuilderModule.cs | 4 ++++ src/MfGames.ToolBuilder/ToolService.cs | 10 ++++++---- 5 files changed, 34 insertions(+), 16 deletions(-) rename src/MfGames.ToolBuilder/{ConfigToolService.cs => Globals/ConfigToolGlobalService.cs} (94%) rename src/MfGames.ToolBuilder/{LoggingToolService.cs => Globals/LoggingToolGlobalService.cs} (95%) diff --git a/src/MfGames.ToolBuilder/ConfigToolService.cs b/src/MfGames.ToolBuilder/Globals/ConfigToolGlobalService.cs similarity index 94% rename from src/MfGames.ToolBuilder/ConfigToolService.cs rename to src/MfGames.ToolBuilder/Globals/ConfigToolGlobalService.cs index cfb5656..2534b30 100644 --- a/src/MfGames.ToolBuilder/ConfigToolService.cs +++ b/src/MfGames.ToolBuilder/Globals/ConfigToolGlobalService.cs @@ -4,14 +4,14 @@ using System.IO; using Microsoft.Extensions.Configuration; -namespace MfGames.ToolBuilder +namespace MfGames.ToolBuilder.Globals { /// /// A utility class for handling `--config` options. /// - public class ConfigToolService + public class ConfigToolGlobalService { - public ConfigToolService() + public ConfigToolGlobalService() { this.ConfigOption = new Option( "--config", @@ -44,7 +44,7 @@ namespace MfGames.ToolBuilder // If we don't have an internal name, blow up. string? internalName = this.InternalName; - if (internalName == null) + if (string.IsNullOrWhiteSpace(internalName)) { throw new ApplicationException( "Cannot determine the default configuration path unless internal name has been set."); @@ -117,7 +117,7 @@ namespace MfGames.ToolBuilder /// The internal name of the application. /// /// The service for chaining operations. - public ConfigToolService WithInternalName(string internalName) + public ConfigToolGlobalService WithInternalName(string internalName) { this.InternalName = internalName; return this; diff --git a/src/MfGames.ToolBuilder/LoggingToolService.cs b/src/MfGames.ToolBuilder/Globals/LoggingToolGlobalService.cs similarity index 95% rename from src/MfGames.ToolBuilder/LoggingToolService.cs rename to src/MfGames.ToolBuilder/Globals/LoggingToolGlobalService.cs index 9cd7d72..a6ff6e8 100644 --- a/src/MfGames.ToolBuilder/LoggingToolService.cs +++ b/src/MfGames.ToolBuilder/Globals/LoggingToolGlobalService.cs @@ -8,14 +8,14 @@ using Serilog.Core; using Serilog.Events; using Serilog.Exceptions; -namespace MfGames.ToolBuilder +namespace MfGames.ToolBuilder.Globals { /// /// A service for handling logging options. /// - public class LoggingToolService + public class LoggingToolGlobalService { - public LoggingToolService() + public LoggingToolGlobalService() { this.LogLevelOption = new Option( "--log-level", diff --git a/src/MfGames.ToolBuilder/ToolBuilder.cs b/src/MfGames.ToolBuilder/ToolBuilder.cs index e8919aa..cbd41f2 100644 --- a/src/MfGames.ToolBuilder/ToolBuilder.cs +++ b/src/MfGames.ToolBuilder/ToolBuilder.cs @@ -5,6 +5,8 @@ using System.Threading.Tasks; using Autofac; using Autofac.Extensions.DependencyInjection; +using MfGames.ToolBuilder.Globals; + using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -22,11 +24,11 @@ namespace MfGames.ToolBuilder { private readonly string[] arguments; - private readonly ConfigToolService configService; + private readonly ConfigToolGlobalService configService; private readonly IHostBuilder hostBuilder; - private readonly LoggingToolService loggingService; + private readonly LoggingToolGlobalService loggingService; public ToolBuilder( string applicationName, @@ -37,9 +39,9 @@ namespace MfGames.ToolBuilder this.arguments = arguments; this.ApplicationName = applicationName; this.InternalName = internalName; - this.configService = new ConfigToolService() + this.configService = new ConfigToolGlobalService() .WithInternalName(this.InternalName); - this.loggingService = new LoggingToolService(); + this.loggingService = new LoggingToolGlobalService(); // Set up logging first so we can report the loading process. This // sets up the Serilog.Log.Logger which means we can use that for @@ -136,6 +138,16 @@ namespace MfGames.ToolBuilder AppDomain.CurrentDomain.ProcessExit += (_, _) => Log.CloseAndFlush(); + // Register the global services as singletons. + builder + .RegisterInstance(this.configService) + .AsSelf() + .SingleInstance(); + builder + .RegisterInstance(this.loggingService) + .AsSelf() + .SingleInstance(); + // Register the components required to make the CLI work. builder.RegisterModule(); } diff --git a/src/MfGames.ToolBuilder/ToolBuilderModule.cs b/src/MfGames.ToolBuilder/ToolBuilderModule.cs index 51398fb..3c7cef4 100644 --- a/src/MfGames.ToolBuilder/ToolBuilderModule.cs +++ b/src/MfGames.ToolBuilder/ToolBuilderModule.cs @@ -1,5 +1,7 @@ using Autofac; +using MfGames.ToolBuilder.Globals; + namespace MfGames.ToolBuilder { /// @@ -13,6 +15,8 @@ namespace MfGames.ToolBuilder builder .RegisterAssemblyTypes(this.GetType().Assembly) .Except() + .Except() + .Except() .AsSelf() .AsImplementedInterfaces(); } diff --git a/src/MfGames.ToolBuilder/ToolService.cs b/src/MfGames.ToolBuilder/ToolService.cs index aea327a..d699b04 100644 --- a/src/MfGames.ToolBuilder/ToolService.cs +++ b/src/MfGames.ToolBuilder/ToolService.cs @@ -7,6 +7,8 @@ using System.CommandLine.Parsing; using System.Threading; using System.Threading.Tasks; +using MfGames.ToolBuilder.Globals; + using Microsoft.Extensions.Hosting; using Serilog; @@ -25,20 +27,20 @@ namespace MfGames.ToolBuilder { private readonly IList commands; - private readonly ConfigToolService configService; + private readonly ConfigToolGlobalService configService; private readonly IHostApplicationLifetime lifetime; private readonly ILogger logger; - private readonly LoggingToolService loggingService; + private readonly LoggingToolGlobalService loggingService; public ToolService( ILogger logger, IHostApplicationLifetime lifetime, IList commands, - ConfigToolService configService, - LoggingToolService loggingService) + ConfigToolGlobalService configService, + LoggingToolGlobalService loggingService) { this.lifetime = lifetime; this.commands = commands;