fix: change config and logging services to be singletons

This commit is contained in:
Dylan R. E. Moonfire 2021-09-11 00:42:04 -05:00
parent bb006290ae
commit 25a65d230d
5 changed files with 34 additions and 16 deletions

View file

@ -4,14 +4,14 @@ using System.IO;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
namespace MfGames.ToolBuilder namespace MfGames.ToolBuilder.Globals
{ {
/// <summary> /// <summary>
/// A utility class for handling `--config` options. /// A utility class for handling `--config` options.
/// </summary> /// </summary>
public class ConfigToolService public class ConfigToolGlobalService
{ {
public ConfigToolService() public ConfigToolGlobalService()
{ {
this.ConfigOption = new Option<string[]>( this.ConfigOption = new Option<string[]>(
"--config", "--config",
@ -44,7 +44,7 @@ namespace MfGames.ToolBuilder
// If we don't have an internal name, blow up. // If we don't have an internal name, blow up.
string? internalName = this.InternalName; string? internalName = this.InternalName;
if (internalName == null) if (string.IsNullOrWhiteSpace(internalName))
{ {
throw new ApplicationException( throw new ApplicationException(
"Cannot determine the default configuration path unless internal name has been set."); "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 internal name of the application.
/// </param> /// </param>
/// <returns>The service for chaining operations.</returns> /// <returns>The service for chaining operations.</returns>
public ConfigToolService WithInternalName(string internalName) public ConfigToolGlobalService WithInternalName(string internalName)
{ {
this.InternalName = internalName; this.InternalName = internalName;
return this; return this;

View file

@ -8,14 +8,14 @@ using Serilog.Core;
using Serilog.Events; using Serilog.Events;
using Serilog.Exceptions; using Serilog.Exceptions;
namespace MfGames.ToolBuilder namespace MfGames.ToolBuilder.Globals
{ {
/// <summary> /// <summary>
/// A service for handling logging options. /// A service for handling logging options.
/// </summary> /// </summary>
public class LoggingToolService public class LoggingToolGlobalService
{ {
public LoggingToolService() public LoggingToolGlobalService()
{ {
this.LogLevelOption = new Option<string>( this.LogLevelOption = new Option<string>(
"--log-level", "--log-level",

View file

@ -5,6 +5,8 @@ using System.Threading.Tasks;
using Autofac; using Autofac;
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using MfGames.ToolBuilder.Globals;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@ -22,11 +24,11 @@ namespace MfGames.ToolBuilder
{ {
private readonly string[] arguments; private readonly string[] arguments;
private readonly ConfigToolService configService; private readonly ConfigToolGlobalService configService;
private readonly IHostBuilder hostBuilder; private readonly IHostBuilder hostBuilder;
private readonly LoggingToolService loggingService; private readonly LoggingToolGlobalService loggingService;
public ToolBuilder( public ToolBuilder(
string applicationName, string applicationName,
@ -37,9 +39,9 @@ namespace MfGames.ToolBuilder
this.arguments = arguments; this.arguments = arguments;
this.ApplicationName = applicationName; this.ApplicationName = applicationName;
this.InternalName = internalName; this.InternalName = internalName;
this.configService = new ConfigToolService() this.configService = new ConfigToolGlobalService()
.WithInternalName(this.InternalName); .WithInternalName(this.InternalName);
this.loggingService = new LoggingToolService(); this.loggingService = new LoggingToolGlobalService();
// Set up logging first so we can report the loading process. This // 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 // sets up the Serilog.Log.Logger which means we can use that for
@ -136,6 +138,16 @@ namespace MfGames.ToolBuilder
AppDomain.CurrentDomain.ProcessExit += AppDomain.CurrentDomain.ProcessExit +=
(_, _) => Log.CloseAndFlush(); (_, _) => 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. // Register the components required to make the CLI work.
builder.RegisterModule<ToolBuilderModule>(); builder.RegisterModule<ToolBuilderModule>();
} }

View file

@ -1,5 +1,7 @@
using Autofac; using Autofac;
using MfGames.ToolBuilder.Globals;
namespace MfGames.ToolBuilder namespace MfGames.ToolBuilder
{ {
/// <summary> /// <summary>
@ -13,6 +15,8 @@ namespace MfGames.ToolBuilder
builder builder
.RegisterAssemblyTypes(this.GetType().Assembly) .RegisterAssemblyTypes(this.GetType().Assembly)
.Except<ToolService>() .Except<ToolService>()
.Except<ConfigToolGlobalService>()
.Except<LoggingToolGlobalService>()
.AsSelf() .AsSelf()
.AsImplementedInterfaces(); .AsImplementedInterfaces();
} }

View file

@ -7,6 +7,8 @@ using System.CommandLine.Parsing;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MfGames.ToolBuilder.Globals;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Serilog; using Serilog;
@ -25,20 +27,20 @@ namespace MfGames.ToolBuilder
{ {
private readonly IList<ITopCommand> commands; private readonly IList<ITopCommand> commands;
private readonly ConfigToolService configService; private readonly ConfigToolGlobalService configService;
private readonly IHostApplicationLifetime lifetime; private readonly IHostApplicationLifetime lifetime;
private readonly ILogger logger; private readonly ILogger logger;
private readonly LoggingToolService loggingService; private readonly LoggingToolGlobalService loggingService;
public ToolService( public ToolService(
ILogger logger, ILogger logger,
IHostApplicationLifetime lifetime, IHostApplicationLifetime lifetime,
IList<ITopCommand> commands, IList<ITopCommand> commands,
ConfigToolService configService, ConfigToolGlobalService configService,
LoggingToolService loggingService) LoggingToolGlobalService loggingService)
{ {
this.lifetime = lifetime; this.lifetime = lifetime;
this.commands = commands; this.commands = commands;