Command Pattern in C# – Real-Time Example (Encapsulating Requests)

What is Command Pattern?

The Command Pattern is a behavioral design pattern that encapsulates a request as an object.

It separates:

  • Sender of request
  • Receiver of request

👉 This allows requests to be:

  • Queued
  • Logged
  • Undone
  • Executed dynamically

Why Use Command Pattern?

  • Decouple sender and receiver
  • Support undo/redo
  • Queue operations
  • Centralize action execution
  • Improve flexibility

Real-Time Scenario

Button Click Actions:

  • Save
  • Delete
  • Update
  • Create Invoice

👉 Each action becomes a command object.


Real-Time Example – Invoice Command System


Step 1: Command Interface


public interface ICommand
{
    void Execute();
}


Step 2: Receiver


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

    public void DeleteInvoice()
    {
        Console.WriteLine("Invoice Deleted");
    }
}


Step 3: Concrete Commands

Create Invoice Command


public class CreateInvoiceCommand : ICommand
{
    private readonly InvoiceService _invoiceService;

    public CreateInvoiceCommand(InvoiceService invoiceService)
    {
        _invoiceService = invoiceService;
    }

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


Delete Invoice Command


public class DeleteInvoiceCommand : ICommand
{
    private readonly InvoiceService _invoiceService;

    public DeleteInvoiceCommand(InvoiceService invoiceService)
    {
        _invoiceService = invoiceService;
    }

    public void Execute()
    {
        _invoiceService.DeleteInvoice();
    }
}


Step 4: Invoker


public class Button
{
    private readonly ICommand _command;

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

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


Usage Example


var service = new InvoiceService();

ICommand createCommand =
    new CreateInvoiceCommand(service);

var button = new Button(createCommand);

button.Click();


Output


Invoice Created


Key Concept

Instead of:


button.Click += CreateInvoice;

We do:


button.Click();
command.Execute();

👉 Request wrapped as object.


Diagram Understanding


Client
   ↓
Command
   ↓
Receiver

👉 Command acts as intermediary.


Advantages

  • ✔ Loose coupling
  • ✔ Easy undo/redo support
  • ✔ Queue and schedule operations
  • ✔ Flexible command management
  • ✔ Easier logging and auditing

Disadvantages

  • ✖ More classes
  • ✖ Increased complexity
  • ✖ Overkill for simple actions

When to Use

Use Command Pattern when:

  • Actions need undo/redo
  • Requests need queuing
  • Decoupling sender/receiver is important
  • Actions should be logged or scheduled

Real Project Mapping (.NET + Angular)

Feature Usage
Button click actions Command
Undo/Redo Command
Background jobs Command
Queue processing Command
CQRS Commands Command

ASP.NET Core Real Example

CQRS + MediatR


CreateInvoiceCommand
DeleteInvoiceCommand
UpdateInvoiceCommand

👉 Each request represented as command object.


Advanced Example – Undo Support


public interface ICommand
{
    void Execute();
    void Undo();
}

👉 Useful in editors and workflows.


Command vs Strategy

Command Strategy
Encapsulates request/action Encapsulates algorithm
Focus on execution Focus on behavior
Can queue/log/undo Runtime behavior switching

Pro Tip

Command Pattern works well with:

  • CQRS
  • MediatR
  • Background jobs
  • Workflow engines
  • Task schedulers

Summary

Command Pattern helps you:

  • Encapsulate requests as objects
  • Decouple sender and receiver
  • Support undo, queue, logging

👉 Perfect for:

  • CQRS
  • Button actions
  • Workflow systems
  • Task execution pipelines

Comments

Popular posts from this blog

Promises in Angular

Debouncing & Throttling in RxJS: Optimizing API Calls and User Interactions

Csharp Coding - Session