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;
namespace MfGames.ToolBuilder
namespace MfGames.ToolBuilder.Globals
{
/// <summary>
/// A utility class for handling `--config` options.
/// </summary>
public class ConfigToolService
public class ConfigToolGlobalService
{
public ConfigToolService()
public ConfigToolGlobalService()
{
this.ConfigOption = new Option<string[]>(
"--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.
/// </param>
/// <returns>The service for chaining operations.</returns>
public ConfigToolService WithInternalName(string internalName)
public ConfigToolGlobalService WithInternalName(string internalName)
{
this.InternalName = internalName;
return this;

View file

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

View file

@ -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<ToolBuilderModule>();
}

View file

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

View file

@ -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<ITopCommand> 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<ITopCommand> commands,
ConfigToolService configService,
LoggingToolService loggingService)
ConfigToolGlobalService configService,
LoggingToolGlobalService loggingService)
{
this.lifetime = lifetime;
this.commands = commands;