mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Refactor CommandFactory
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
/// <summary>
|
||||
/// Configures application to use specified factory method for creating new instances of <see cref="ICommand"/>.
|
||||
/// </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));
|
||||
factoryMethod.GuardNotNull(nameof(factoryMethod));
|
||||
|
||||
@@ -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
|
||||
{
|
||||
/// <inheritdoc />
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using CliFx.Internal;
|
||||
using CliFx.Models;
|
||||
|
||||
namespace CliFx.Services
|
||||
{
|
||||
@@ -8,21 +9,21 @@ namespace CliFx.Services
|
||||
/// </summary>
|
||||
public class DelegateCommandFactory : ICommandFactory
|
||||
{
|
||||
private readonly Func<Type, ICommand> _factoryMethod;
|
||||
private readonly Func<CommandSchema, ICommand> _factoryMethod;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="DelegateCommandFactory"/>.
|
||||
/// </summary>
|
||||
public DelegateCommandFactory(Func<Type, ICommand> factoryMethod)
|
||||
public DelegateCommandFactory(Func<CommandSchema, ICommand> factoryMethod)
|
||||
{
|
||||
_factoryMethod = factoryMethod.GuardNotNull(nameof(factoryMethod));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public ICommand CreateCommand(Type commandType)
|
||||
public ICommand CreateCommand(CommandSchema commandSchema)
|
||||
{
|
||||
commandType.GuardNotNull(nameof(commandType));
|
||||
return _factoryMethod(commandType);
|
||||
commandSchema.GuardNotNull(nameof(commandSchema));
|
||||
return _factoryMethod(commandSchema);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using CliFx.Models;
|
||||
|
||||
namespace CliFx.Services
|
||||
{
|
||||
@@ -8,8 +9,8 @@ namespace CliFx.Services
|
||||
public interface ICommandFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="ICommand"/> of specified type.
|
||||
/// Initializes an instance of <see cref="ICommand"/> with specified schema.
|
||||
/// </summary>
|
||||
ICommand CreateCommand(Type commandType);
|
||||
ICommand CreateCommand(CommandSchema commandSchema);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user