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
Creates an empty CommandSet object.
CommandSet()
Instance methods
add_command
Adds a command to this command set.
See ICommand
add_command(command: ICommand)
- command: ICommand - the command to add.
add_command_set
Adds all of the commands and events from specified command set into this one.
add_command_set(command_set: CommandSet)
- command_set: CommandSet - the CommandSet to add.
add_commands
Adds multiple commands to this command set.
See ICommand
add_commands(commands: List[ICommand])
- commands: List[ICommand] - the array of commands to add.
add_event
Adds an event to this command set.
See IEvent
add_event(event: IEvent)
- event: IEvent - the event to add.
add_events
Adds multiple events to this command set.
See IEvent
add_events(events: List[IEvent])
- event: List[IEvent] - the array of events to add.
add_interceptor
Adds a command interceptor to this command set.
add_interceptor(interceptor: ICommandInterceptor)
- interceptor: ICommandInterceptor the interceptor to add.
add_listener
Adds a listener to receive notifications on fired events.
See IEventListener
add_listener(listener: IEventListener)
- listener: IEventListener - the listener to add.
execute
Executes a command specificed by its name.
See ICommand, Parameters
execute(context: Optional[IContext], command: str, args: Parameters): Any
- context: IContext - (optional) a context to trace execution through a call chain.
- command_name: str - the name of that command that is to be executed.
- args: Parameters - the parameters (arguments) to pass to the command for execution.
- returns: Any - the execution result
find_command
Searches for a command by its name.
See ICommand
find_command(command_name: str): ICommand
- command_name: ICommand - the name of the command to search for.
- returns: ICommand - the command, whose name matches the provided name.
find_event
Searches for an event by its name in this command set.
find_event(event_name: str): Optional[IEvent]
- event_name: str - the name of the event to search for.
- returns: Optional[IEvent] - the event, whose name matches the provided name.
get_commands
Gets all commands registered in this command set.
See ICommand
get_commands(): List[ICommand]
- returns: List[ICommand] - a list of commands.
get_events
Gets all events registred in this command set.
See IEvent
get_events(): List[IEvent]
- returns: List[IEvent] - a list of events.
notify
Fires event specified by its name and notifies all registered listeners
notify(context: Optional[IContext], event_name: str, args: Parameters)
- context: IContext - (optional) a context to trace execution through a call chain.
- event_name: str - the name of the event that is to be fired.
- args: Parameters - the event arguments (parameters).
remove_listener
Removes previosly added listener.
See IEventListener
remove_listener(listener: IEventListener)
- listener: IEventListener - the listener to remove.
validate
Validates args for command specified by its name using a defined schema. If validation schema is not defined, then the methods returns no errors. It returns a validation error if the command is not found.
validate(command_name: str, args: Parameters): List[ValidationResult]
- command_name: str - the name of the command for which the ‘args’ must be validated.
- args: Parameters - the parameters (arguments) to validate.
- returns: List[ValidationResult] - an 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
class MyDataCommandSet(CommandSet):
_controller = None
def __init__(self, controller):
super(MyDataCommandSet, self).__init__()
self._controller = controller
self.add_command(self._make_get_my_data_command())
def _make_get_my_data_command(self):
def handler(context, args):
param = args.get_as_string('param')
return self._controller.get_my_data(context, param)
return Command(
"get_mydata",
None,
handler
)