Command

Command allows to call a method or a function.

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 - the command name.
  • schema: Schema - the schema to validate command arguments.
  • action: ExecutableDelegate - the function to be executed by this command.

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(string correlationId, Parameters args)

  • correlationId: string - (optional) transaction id to trace execution through call chain.
  • args: Parameters - the parameters (arguments) to pass to this command for execution.
  • returns: Task<object> - the execution result

validate

Validates the command args before execution using the defined schema.

public IList<ValidationResult> validate(Parameters args)

  • args: Parameters - the parameters (arguments) to validate using this command’s schema.
  • returns: IList<ValidationResult> - an 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

See also