Command Pattern in C# – Real-Time Example (Action Handling in Applications)

What is Command Pattern?

The Command Pattern is a behavioral design pattern that encapsulates a request as an object, allowing you to parameterize, queue, or log operations.

Instead of calling methods directly, you wrap them inside command objects.


Why Use Command Pattern?

  • Decouple sender (UI/API) from receiver (business logic)
  • Enable undo/redo operations
  • Support logging, queuing, and auditing
  • Make code extensible and maintainable

Real-Time Scenario

In an application:

  • Button click → Save / Delete / Update
  • API request → Create / Update / Cancel Invoice

Instead of directly calling service methods, we use commands.


Implementation

Step 1: Command Interface


public interface ICommand
{
    void Execute();
}

Step 2: Concrete Command


public class CreateInvoiceCommand : ICommand
{
    public void Execute()
    {
        Console.WriteLine("Invoice Created");
    }
}

Step 3: Usage


ICommand command = new CreateInvoiceCommand();
command.Execute();

Real-World Improved Example (Service Integration)

Receiver (Business Logic)


public class InvoiceService
{
    public void CreateInvoice()
    {
        Console.WriteLine("Invoice Created in DB");
    }
}

Command with Receiver


public class CreateInvoiceCommand : ICommand
{
    private readonly InvoiceService _service;

    public CreateInvoiceCommand(InvoiceService service)
    {
        _service = service;
    }

    public void Execute()
    {
        _service.CreateInvoice();
    }
}

Invoker (Caller)


public class Button
{
    private readonly ICommand _command;

    public Button(ICommand command)
    {
        _command = command;
    }

    public void Click()
    {
        _command.Execute();
    }
}

Final Usage


var service = new InvoiceService();
var command = new CreateInvoiceCommand(service);

var button = new Button(command);
button.Click();

Key Concept

Instead of:


invoiceService.CreateInvoice();

We do:


ICommand command = new CreateInvoiceCommand(service);
command.Execute();

👉 Request is now an object


Advantages

  • Loose coupling between sender and receiver
  • Easy to add new commands (no modification needed)
  • Supports undo/redo operations
  • Enables logging and queuing

Disadvantages

  • Increases number of classes
  • Can be overkill for simple actions
  • Adds abstraction layer

When to Use

  • When actions need to be parameterized
  • When implementing undo/redo functionality
  • When you need queue, logging, or history
  • When building UI or API action handling systems

Real Project Mapping (.NET + Angular)

Feature Usage
Button click (UI) Command
API request (POST/PUT) Command
CQRS Write Operations Command
Background jobs Command

Pro Tip (Advanced .NET Usage)

  • Use MediatR for command handling in CQRS
  • Each command → separate handler
  • Clean separation of responsibilities

Example:


public record CreateInvoiceCommand(string Customer, decimal Amount);

Summary

Command Pattern helps you:

  • Encapsulate actions as objects
  • Decouple UI/API from business logic
  • Build scalable and maintainable systems

👉 Perfect for API actions, UI commands, CQRS, background processing

Comments