CommandSet

Contains a set of commands and events supported by a commandable object. The CommandSet supports command interceptors and command call chains.

Description

The CommandSet class allows you to create a set of commands and events supported by a commandable object. In addition, it supports command interceptors and command call chains.

Important points

  • CommandSets can be used as an alternative commandable interface to a business object.
  • This class can be used to auto generate multiple external services for a business object.

Constructors

NewCommandSet

Creates an empty CommandSet object.

NewCommandSet() *CommandSet

Methods

AddCommand

Adds a command to this command set.
See ICommand

(c *CommandSet) AddCommand(command ICommand)

AddCommandSet

Adds all of the commands and events from specified command set into this one.

(c *CommandSet) AddCommandSet(commandSet *CommandSet)

AddCommands

Adds multiple commands to this command set.
See ICommand

(c *CommandSet) AddCommands(commands []ICommand)

  • commands: []ICommand - array of commands to add.

AddEvent

Adds an event to this command set.
See IEvent

(c *CommandSet) AddEvent(event IEvent)

  • event: IEvent - event to add.

AddEvents

Adds multiple events to this command set.
See IEvent

(c [*CommandSet]) AddEvents(events []IEvent)

  • event: []IEvent - array of events to add.

AddInterceptor

Adds a command interceptor to this command set.

(c *CommandSet) AddInterceptor(interceptor ICommandInterceptor)

AddListener

Adds a listener to receive notifications on fired events.
See IEventListener

(c *CommandSet) AddListener(listener IEventListener)

Execute

Executes a command specificed by its name.
See ICommand, Parameters

(c *CommandSet) Execute(correlationId string, commandName string, args *run.Parameters) (result interface{}, err error)

  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • commandName: string - name of that command to be executed.
  • args: *run.Parameters - parameters (arguments) to pass to the command for execution.
  • returns:
    • result interface{} - execution result
    • err: error

FindCommand

Searches for a command by its name.
See ICommand

(c *CommandSet) FindCommand(commandName string) ICommand

  • commandName: ICommand - name of the command to search for.
  • returns: ICommand - command, whose name matches the provided name.

FindEvent

Searches for an event by its name in this command set.

(c *CommandSet) FindEvent(eventName string) IEvent

  • eventName: string - name of the event to search for.
  • returns: IEvent - event, whose name matches the provided name.

Commands

Gets all commands registered in this command set.
See ICommand

(c *CommandSet) Commands() []ICommand

  • returns: []ICommand - list of commands.

Events

Gets all events registred in this command set.
See IEvent

(c *CommandSet) Events() []IEvent

  • returns: []IEvent - list of events.

Notify

Fires an event specified by its name and notifies all registered listeners

(c *CommandSet) Notify(correlationId string, eventName string, args *run.Parameters)

  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • eventName: string - name of the event that is to be fired.
  • args: *run.Parameters - event’s arguments (parameters).

RemoveListener

Removes previosly added listener.
See IEventListener

(c *CommandSet) RemoveListener(listener IEventListener)

Validate

Validates args for command specified by its name using defined schema. If validation schema is not defined, then the methods returns no errors. It returns validation error if the command is not found.

(c *CommandSet) Validate(commandName string, args *run.Parameters) []*validate.ValidationResult

  • commandName: string - name of the command for which the ‘args’ must be validated.
  • args: *run.Parameters - parameters (arguments) to validate.
  • returns: []*validate.ValidationResult - array of ValidationResults. If no command is found by the given name, then the returned array of ValidationResults will contain a single entry, whose type will be ValidationResultType.Error.

Examples

type MyDataCommandSet struct {
	*CommandSet
	_controller IMyDataController
}

// Any data controller interface
func (dcs *MyDataCommandSet) CreateMyDataCommandSet(controller IMyDataController) {
	dcs._controller = controller
	dcs.AddCommand(dcs.makeGetMyDataCommand())
}

func (dcs *MyDataCommandSet) makeGetMyDataCommand() ICommand {
	return NewCommand(
		"get_mydata",
		nil,
		func(ctx context.Context, correlationId string, args *run.Parameters) (any, err) {
			var param = args.GetAsString("param")
			return dcs._controller.GetMyData(correlationId, param)
		},
	)
}

See also