Use IServiceProvider in Help command

This commit is contained in:
2022-12-19 18:56:39 +01:00
parent b33568cd67
commit e75f853e6a
3 changed files with 6 additions and 6 deletions

View File

@@ -9,9 +9,9 @@ public class Application
private readonly IList<ICommand> _commands; private readonly IList<ICommand> _commands;
private readonly ILogger<Application> _logger; private readonly ILogger<Application> _logger;
public Application(IEnumerable<ICommand> commands, ILogger<Application> logger, ILogger<HelpCommand> helpCommandLogger) public Application(IEnumerable<ICommand> commands, ILogger<Application> logger)
{ {
_commands = commands.Append(new HelpCommand(() => _commands!, helpCommandLogger)).ToList(); _commands = commands.ToList();
_logger = logger; _logger = logger;
} }

View File

@@ -10,11 +10,11 @@ public class HelpCommand : ICommand
public string CommandString => "help"; public string CommandString => "help";
public HelpCommand( public HelpCommand(
Func<IEnumerable<ICommand>> commandsProvider, IServiceProvider serviceProvider,
ILogger<HelpCommand> logger ILogger<HelpCommand> logger
) )
{ {
_commandsProvider = commandsProvider; _commandsProvider = () => (IEnumerable<ICommand>?)serviceProvider.GetService(typeof(IEnumerable<ICommand>)) ?? throw new ApplicationException();
_logger = logger; _logger = logger;
} }

View File

@@ -1,4 +1,5 @@
using Alma.Command; using Alma.Command;
using Alma.Command.Help;
using Alma.Command.Info; using Alma.Command.Info;
using Alma.Command.Install; using Alma.Command.Install;
using Alma.Command.Link; using Alma.Command.Link;
@@ -45,8 +46,7 @@ public static class Program
[Singleton(typeof(ICommand), typeof(InfoCommand))] [Singleton(typeof(ICommand), typeof(InfoCommand))]
[Singleton(typeof(ICommand), typeof(ListCommand))] [Singleton(typeof(ICommand), typeof(ListCommand))]
[Singleton(typeof(ICommand), typeof(InstallCommand))] [Singleton(typeof(ICommand), typeof(InstallCommand))]
//Dependency cycle [Singleton(typeof(ICommand), typeof(HelpCommand))]
//[Singleton(typeof(ICommand), typeof(HelpCommand))]
[Singleton(typeof(IModuleConfigurationResolver), typeof(ModuleConfigurationResolver))] [Singleton(typeof(IModuleConfigurationResolver), typeof(ModuleConfigurationResolver))]
[Singleton(typeof(IMetadataHandler), typeof(MetadataHandler))] [Singleton(typeof(IMetadataHandler), typeof(MetadataHandler))]
[Singleton(typeof(IShellService), typeof(ShellService))] [Singleton(typeof(IShellService), typeof(ShellService))]