InterceptedCommand

Implements a command wrapped by an interceptor.

Inherits: 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.

public InterceptedCommand(ICommandInterceptor interceptor, ICommand next)

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

Properties

Name

Gets the command name.

public string Name [ get ]

Schema

Gets the command schema.

public Schema Schema [ get ]

Instance methods

ExecuteAsync

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

public Task<object> ExecuteAsync(string correlationId, Parameters args)

  • correlationId: string - unique transaction id to trace calls across components.
  • args: Parameters - the parameters (arguments) to pass to the command for execution.
  • returns: Task<object> - the execution result

Validate

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

public IList<ValidationResult> Validate(Parameters args)

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

Examples

public class CommandLogger: ICommandInterceptor
{
    public String GetName(ICommand command) 
    {
        return command.GetName();
    }
    
    public Task<object> ExecuteAsync(string correlationId, ICommand command, Parameters args) 
    {
        Console.WriteLine("Executed command " + command.getName());
        return command.ExecuteAsync(correlationId, args); 
    }
    
    private IList<ValidationResult> validate(ICommand command, Parameters args) 
    {
        return command.validate(args);
    }
}
var logger = new CommandLogger();
var loggedCommand = new InterceptedCommand(logger, command);
// Each called command will output: Executed command <command name>

See also