InterceptedCommand

Implements a command wrapped by an interceptor.

Implements: ICommand

Description

The InterceptedCommand allows you to implement a command wrapped by an interceptor. Thus, it allows you to build command call chains, where the interceptor can alter execution and delegate calls to a next command, which can then be intercepted or not.

Constructors

Creates a new InterceptedCommand, which serves as a link in an execution chain. Contains information about the interceptor that is being used and the next command in the chain.

InterceptedCommand(ICommandInterceptor interceptor, ICommand next)

  • interceptor: ICommandInterceptor - interceptor that is intercepting the command.
  • next: ICommand - (link to) the next command in the command’s execution chain.

Instance methods

execute

Executes the next command in the execution chain using the given parameters (arguments).

See Parameters

@override

Future<dynamic> execute(String? correlationId, Parameters args)

  • correlationId: String? - unique transaction id used to trace calls across components.
  • args: Parameters - parameters (arguments) to pass to the command for execution.
  • returns: Future<dynamic> - execution result

getName

Returns a string with the name of the command that is being intercepted.

@override

String getName()

  • returns: String - name of the command that is being intercepted.

validate

Validates the parameters (arguments) that are to be passed to the command that is next in the execution chain.

See Parameters, ValidationResult

@override

List<ValidationResult> validate(Parameters args)

  • args: Parameters - parameters (arguments) to validate for the next command.
  • returns: List<ValidationResult> - array of ValidationResults.

Examples

class CommandLogger implements ICommandInterceptor {
    String getName(ICommand command) {
        return command.getName();
    }
    void execute(String? correlationId, ICommand command , Parameters args,) async {
        print('Executed command ' + command.getName());
        return await command.execute(correlationId, args);
    }
    List<ValidationResult> validate(ICommand command, Parameters args) {
        return command.validate(args);
    }
}
var logger =  CommandLogger();
var loggedCommand =  InterceptedCommand(logger, command);

// Each called command will output: Executed command <command name>

See also