Refactor CommandFactory

This commit is contained in:
Alexey Golub
2019-08-19 01:20:01 +03:00
parent 66f9b1a256
commit a9f4958c92
8 changed files with 20 additions and 15 deletions

View File

@@ -25,7 +25,7 @@ namespace CliFx.Demo
return new CliApplicationBuilder() return new CliApplicationBuilder()
.AddCommandsFromThisAssembly() .AddCommandsFromThisAssembly()
.UseCommandFactory(type => (ICommand) serviceProvider.GetRequiredService(type)) .UseCommandFactory(schema => (ICommand) serviceProvider.GetRequiredService(schema.Type))
.Build() .Build()
.RunAsync(args); .RunAsync(args);
} }

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using CliFx.Attributes; using CliFx.Attributes;
using CliFx.Models;
using CliFx.Services; using CliFx.Services;
using FluentAssertions; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
@@ -33,7 +34,7 @@ namespace CliFx.Tests
var factory = new CommandFactory(); var factory = new CommandFactory();
// Act // Act
var command = factory.CreateCommand(commandType); var command = factory.CreateCommand(new CommandSchema(commandType, null, null, new CommandOptionSchema[0]));
// Assert // Assert
command.Should().BeOfType(commandType); command.Should().BeOfType(commandType);

View File

@@ -107,7 +107,7 @@ namespace CliFx
} }
// Create an instance of the command // 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 // Populate command with options according to its schema
_commandInitializer.InitializeCommand(command, targetCommandSchema, commandInput); _commandInitializer.InitializeCommand(command, targetCommandSchema, commandInput);

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using CliFx.Internal; using CliFx.Internal;
using CliFx.Models;
using CliFx.Services; using CliFx.Services;
namespace CliFx namespace CliFx
@@ -51,7 +52,7 @@ namespace CliFx
/// <summary> /// <summary>
/// Configures application to use specified factory method for creating new instances of <see cref="ICommand"/>. /// Configures application to use specified factory method for creating new instances of <see cref="ICommand"/>.
/// </summary> /// </summary>
public static ICliApplicationBuilder UseCommandFactory(this ICliApplicationBuilder builder, Func<Type, ICommand> factoryMethod) public static ICliApplicationBuilder UseCommandFactory(this ICliApplicationBuilder builder, Func<CommandSchema, ICommand> factoryMethod)
{ {
builder.GuardNotNull(nameof(builder)); builder.GuardNotNull(nameof(builder));
factoryMethod.GuardNotNull(nameof(factoryMethod)); factoryMethod.GuardNotNull(nameof(factoryMethod));

View File

@@ -1,5 +1,6 @@
using System; using System;
using CliFx.Internal; using CliFx.Internal;
using CliFx.Models;
namespace CliFx.Services namespace CliFx.Services
{ {
@@ -9,10 +10,10 @@ namespace CliFx.Services
public class CommandFactory : ICommandFactory public class CommandFactory : ICommandFactory
{ {
/// <inheritdoc /> /// <inheritdoc />
public ICommand CreateCommand(Type commandType) public ICommand CreateCommand(CommandSchema commandSchema)
{ {
commandType.GuardNotNull(nameof(commandType)); commandSchema.GuardNotNull(nameof(commandSchema));
return (ICommand) Activator.CreateInstance(commandType); return (ICommand) Activator.CreateInstance(commandSchema.Type);
} }
} }
} }

View File

@@ -1,5 +1,6 @@
using System; using System;
using CliFx.Internal; using CliFx.Internal;
using CliFx.Models;
namespace CliFx.Services namespace CliFx.Services
{ {
@@ -8,21 +9,21 @@ namespace CliFx.Services
/// </summary> /// </summary>
public class DelegateCommandFactory : ICommandFactory public class DelegateCommandFactory : ICommandFactory
{ {
private readonly Func<Type, ICommand> _factoryMethod; private readonly Func<CommandSchema, ICommand> _factoryMethod;
/// <summary> /// <summary>
/// Initializes an instance of <see cref="DelegateCommandFactory"/>. /// Initializes an instance of <see cref="DelegateCommandFactory"/>.
/// </summary> /// </summary>
public DelegateCommandFactory(Func<Type, ICommand> factoryMethod) public DelegateCommandFactory(Func<CommandSchema, ICommand> factoryMethod)
{ {
_factoryMethod = factoryMethod.GuardNotNull(nameof(factoryMethod)); _factoryMethod = factoryMethod.GuardNotNull(nameof(factoryMethod));
} }
/// <inheritdoc /> /// <inheritdoc />
public ICommand CreateCommand(Type commandType) public ICommand CreateCommand(CommandSchema commandSchema)
{ {
commandType.GuardNotNull(nameof(commandType)); commandSchema.GuardNotNull(nameof(commandSchema));
return _factoryMethod(commandType); return _factoryMethod(commandSchema);
} }
} }
} }

View File

@@ -1,4 +1,5 @@
using System; using System;
using CliFx.Models;
namespace CliFx.Services namespace CliFx.Services
{ {
@@ -8,8 +9,8 @@ namespace CliFx.Services
public interface ICommandFactory public interface ICommandFactory
{ {
/// <summary> /// <summary>
/// Initializes an instance of <see cref="ICommand"/> of specified type. /// Initializes an instance of <see cref="ICommand"/> with specified schema.
/// </summary> /// </summary>
ICommand CreateCommand(Type commandType); ICommand CreateCommand(CommandSchema commandSchema);
} }
} }

View File

@@ -139,7 +139,7 @@ public static class Program
return new CliApplicationBuilder() return new CliApplicationBuilder()
.AddCommandsFromThisAssembly() .AddCommandsFromThisAssembly()
.UseCommandFactory(type => (ICommand) serviceProvider.GetRequiredService(type)) .UseCommandFactory(schema => (ICommand) serviceProvider.GetRequiredService(schema.Type))
.Build() .Build()
.RunAsync(args); .RunAsync(args);
} }