SOLID & Design Patterns Interview Question #38
What are common advantages of the Command Pattern?
Structural, Creational & Behavioral Patterns Junior Beginner
Quick Interview Answer
Commands make application actions explicit and can separate invocation from execution. This can simplify validation, logging, authorization, transactions, queues, retries, and testing around individual use cases.
Detailed Explanation
Commands make application actions explicit and can separate invocation from execution. This can simplify validation, logging, authorization, transactions, queues, retries, and testing around individual use cases.
The tradeoff is additional types and indirection. A small CRUD application does not need a separate command class and handler for every trivial operation unless the structure provides real value.
Code Example
public class CreateOrderHandler
: ICommandHandler<CreateOrderCommand>
{
public Task HandleAsync(CreateOrderCommand command)
{
// Execute the use case.
return Task.CompletedTask;
}
}