Inherits: ICommand
Description
The Command class allows you to call a method or a function.
Constructors
Creates a new command object and assigns it’s parameters.
public
Command(string name, Schema schema, ExecutableDelegate function)
- name: string - command name.
- schema: Schema - schema to validate command arguments.
- action: ExecutableDelegate - function to be executed by this command.
Delegate Constructors
nitializes a delegate that invokes the specified instance method.
ExecutableDelegate(IContext context, Parameters args)
- context: IContext - (optional) a context to trace execution through a call chain.
- args: Parameters - parameters
Properties
Name
Gets the command name.
public
string Name { get; }
Schema
Gets the command schema.
public
Schema Schema { get; }
Instance methods
ExecuteAsync
Executes the command. Before execution it validates args using the defined schema.
Raise ApplicationException when execution fails for whatever reason.
See Parameters
public
Task<object> ExecuteAsync(IContext context, Parameters args)
- context: IContext - (optional) a context to trace execution through a call chain.
- args: Parameters - parameters (arguments) used to pass to this command for execution.
- returns: Task<object> - execution result
validate
Validates the command args before execution using the defined schema.
public
IList<ValidationResult> validate(Parameters args)
- args: Parameters - parameters (arguments) used to validate using this command’s schema.
- returns: IList<ValidationResult> - array of ValidationResults or an empty array (if no schema is set).
Examples
var command = new Command("add", null, async(args) => {
var param1 = args.GetAsFloat("param1");
var param2 = args.GetAsFloat("param2");
return param1 + param2; });
var result = command.ExecuteAsync("123", Parameters.FromTuples(
"param1", 2,
"param2", 2 ));
Console.WriteLine(result.ToString());
// Console output: 4