Console Application. Commands Implementation

Before implementation of the new Command, add Xafari.BC.dll assembly to the module. Implement ICommandEnumerator interface in the module class. This interface exposes only one Commands property of the IEnumerable type.

Code snippet below demonstrates Commands property where Command is implemented in the DemoCommand class:

  • c#
  • VB

public IEnumerable<ICommand> Commands
    yield return new DemoCommand();

Public ReadOnly Property Commands As IEnumerable(Of ICommand)
  End Get
End Property

Encode each Command in a individual special class that implements ICommand or ICommandExtIO interfaces. These interfaces expose the following members:

  • Description property specifies explanatory text, user will see description when call the Command help.
  • Name property specifies the Command name. Spaces are not allowed. Use simple and short names to ease console input.
  • ParametersDescription property specifies the collection of CommandParameterDescription items, each item describes the individual Command parameter.
  • Out is a TextWriter type property. It provides text output. Use this property in Command code to output text information. Framework initializes this property automatically when running the Command.
  • Execute method is a direct implementation of the algorithm to be executed at start Command. The parameters is a key-value pairs collection and a reference to the instance of the main XAF application.

ICommandExtIO interface extends ICommand with two properties.

  • Error is a TextWriter type property, it serves to displays information about errors that occur during the execution of the Command.
  • In is a TextReader type property, it is intended to read data required for the Command. For example, RunCmd.exe attaches standard Console.In input stream to this property. Thus, the implementation of the Command can read data from the redirected input stream, for example:
  • example

C:\Utils\RunCmd.exe demo < somedata.dat

CommandBase and CommandExtIOBase abstract classes implements respectively ICommand and ICommandExtIO interfaces. It is recommended to use these classes to implement Commands. It already provides useful functionality for more Commands.

ValidateParameters virtual method performs parameters basic validation. Having detected an incorrect parameter, it generates an exception of the ArgumentException type. The method considers all parameters are optional. If necessary, the developer may override this method and implement an additional check on the parameters. For processing of incorrect parameters, it is also necessary to generate an exception of the ArgumentException type or its descendant.

By default, the framework itself initializes Out, Error and In properties, i.e. when the Command is invoked, they will be initialized in any case. For these purposes, framework uses a "stub objects" as a default values. These "stub objects" don't implement nothing logic, but allows the developer don't compare the properties with null before each data output.

When using the above abstract classes, the general Command code placed in the ExecuteCore abstract method. It is possible that the developer implementing ExecuteCore, decides to introduce additional validation of parameters. Then, when detecting incorrect parameters it is required to generate exception of only ArgumentException type or its heirs.