From a9f4958c925707d73e4f78399be153a2024e9cfd Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Mon, 19 Aug 2019 01:20:01 +0300 Subject: [PATCH] Refactor CommandFactory --- CliFx.Demo/Program.cs | 2 +- CliFx.Tests/CommandFactoryTests.cs | 3 ++- CliFx/CliApplication.cs | 2 +- CliFx/Extensions.cs | 3 ++- CliFx/Services/CommandFactory.cs | 7 ++++--- CliFx/Services/DelegateCommandFactory.cs | 11 ++++++----- CliFx/Services/ICommandFactory.cs | 5 +++-- Readme.md | 2 +- 8 files changed, 20 insertions(+), 15 deletions(-) diff --git a/CliFx.Demo/Program.cs b/CliFx.Demo/Program.cs index 932e34b..6a13bdd 100644 --- a/CliFx.Demo/Program.cs +++ b/CliFx.Demo/Program.cs @@ -25,7 +25,7 @@ namespace CliFx.Demo return new CliApplicationBuilder() .AddCommandsFromThisAssembly() - .UseCommandFactory(type => (ICommand) serviceProvider.GetRequiredService(type)) + .UseCommandFactory(schema => (ICommand) serviceProvider.GetRequiredService(schema.Type)) .Build() .RunAsync(args); } diff --git a/CliFx.Tests/CommandFactoryTests.cs b/CliFx.Tests/CommandFactoryTests.cs index 14f1498..4b5196e 100644 --- a/CliFx.Tests/CommandFactoryTests.cs +++ b/CliFx.Tests/CommandFactoryTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using CliFx.Attributes; +using CliFx.Models; using CliFx.Services; using FluentAssertions; using NUnit.Framework; @@ -33,7 +34,7 @@ namespace CliFx.Tests var factory = new CommandFactory(); // Act - var command = factory.CreateCommand(commandType); + var command = factory.CreateCommand(new CommandSchema(commandType, null, null, new CommandOptionSchema[0])); // Assert command.Should().BeOfType(commandType); diff --git a/CliFx/CliApplication.cs b/CliFx/CliApplication.cs index 2b563eb..2a524cc 100644 --- a/CliFx/CliApplication.cs +++ b/CliFx/CliApplication.cs @@ -107,7 +107,7 @@ namespace CliFx } // Create an instance of the command - var command = _commandFactory.CreateCommand(targetCommandSchema.Type); + var command = _commandFactory.CreateCommand(targetCommandSchema); // Populate command with options according to its schema _commandInitializer.InitializeCommand(command, targetCommandSchema, commandInput); diff --git a/CliFx/Extensions.cs b/CliFx/Extensions.cs index 8ccc1e0..19953f1 100644 --- a/CliFx/Extensions.cs +++ b/CliFx/Extensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Reflection; using CliFx.Internal; +using CliFx.Models; using CliFx.Services; namespace CliFx @@ -51,7 +52,7 @@ namespace CliFx /// /// Configures application to use specified factory method for creating new instances of . /// - public static ICliApplicationBuilder UseCommandFactory(this ICliApplicationBuilder builder, Func factoryMethod) + public static ICliApplicationBuilder UseCommandFactory(this ICliApplicationBuilder builder, Func factoryMethod) { builder.GuardNotNull(nameof(builder)); factoryMethod.GuardNotNull(nameof(factoryMethod)); diff --git a/CliFx/Services/CommandFactory.cs b/CliFx/Services/CommandFactory.cs index 079b35e..261d9a4 100644 --- a/CliFx/Services/CommandFactory.cs +++ b/CliFx/Services/CommandFactory.cs @@ -1,5 +1,6 @@ using System; using CliFx.Internal; +using CliFx.Models; namespace CliFx.Services { @@ -9,10 +10,10 @@ namespace CliFx.Services public class CommandFactory : ICommandFactory { /// - public ICommand CreateCommand(Type commandType) + public ICommand CreateCommand(CommandSchema commandSchema) { - commandType.GuardNotNull(nameof(commandType)); - return (ICommand) Activator.CreateInstance(commandType); + commandSchema.GuardNotNull(nameof(commandSchema)); + return (ICommand) Activator.CreateInstance(commandSchema.Type); } } } \ No newline at end of file diff --git a/CliFx/Services/DelegateCommandFactory.cs b/CliFx/Services/DelegateCommandFactory.cs index f11d2ff..e855b5d 100644 --- a/CliFx/Services/DelegateCommandFactory.cs +++ b/CliFx/Services/DelegateCommandFactory.cs @@ -1,5 +1,6 @@ using System; using CliFx.Internal; +using CliFx.Models; namespace CliFx.Services { @@ -8,21 +9,21 @@ namespace CliFx.Services /// public class DelegateCommandFactory : ICommandFactory { - private readonly Func _factoryMethod; + private readonly Func _factoryMethod; /// /// Initializes an instance of . /// - public DelegateCommandFactory(Func factoryMethod) + public DelegateCommandFactory(Func factoryMethod) { _factoryMethod = factoryMethod.GuardNotNull(nameof(factoryMethod)); } /// - public ICommand CreateCommand(Type commandType) + public ICommand CreateCommand(CommandSchema commandSchema) { - commandType.GuardNotNull(nameof(commandType)); - return _factoryMethod(commandType); + commandSchema.GuardNotNull(nameof(commandSchema)); + return _factoryMethod(commandSchema); } } } \ No newline at end of file diff --git a/CliFx/Services/ICommandFactory.cs b/CliFx/Services/ICommandFactory.cs index cf598b5..8258639 100644 --- a/CliFx/Services/ICommandFactory.cs +++ b/CliFx/Services/ICommandFactory.cs @@ -1,4 +1,5 @@ using System; +using CliFx.Models; namespace CliFx.Services { @@ -8,8 +9,8 @@ namespace CliFx.Services public interface ICommandFactory { /// - /// Initializes an instance of of specified type. + /// Initializes an instance of with specified schema. /// - ICommand CreateCommand(Type commandType); + ICommand CreateCommand(CommandSchema commandSchema); } } \ No newline at end of file diff --git a/Readme.md b/Readme.md index 39186ff..a1ff81a 100644 --- a/Readme.md +++ b/Readme.md @@ -139,7 +139,7 @@ public static class Program return new CliApplicationBuilder() .AddCommandsFromThisAssembly() - .UseCommandFactory(type => (ICommand) serviceProvider.GetRequiredService(type)) + .UseCommandFactory(schema => (ICommand) serviceProvider.GetRequiredService(schema.Type)) .Build() .RunAsync(args); }