Facade Pattern in C# – Real-Time Example (Simplifying Complex Systems)

What is Facade Pattern?

The Facade Pattern is a structural design pattern that provides a simple unified interface to a complex system.

It hides internal complexity and exposes only the required functionality.


Why Use Facade Pattern?

  • Simplify complex workflows
  • Reduce dependency on multiple services
  • Improve readability
  • Provide a clean API layer

Real-Time Scenario

In an Order Management System:

Placing an order requires:

  • Creating order
  • Processing payment
  • Sending notification
  • Updating inventory

👉 Instead of calling all services separately, use a Facade.


Real-Time Example – Order Processing System


Step 1: Subsystem Services

Order Service

public class OrderService
{
    public void CreateOrder()
    {
        Console.WriteLine("Order Created");
    }
}

Payment Service

public class PaymentService
{
    public void ProcessPayment()
    {
        Console.WriteLine("Payment Processed");
    }
}

Notification Service

public class NotificationService
{
    public void SendNotification()
    {
        Console.WriteLine("Notification Sent");
    }
}

Inventory Service

public class InventoryService
{
    public void UpdateStock()
    {
        Console.WriteLine("Inventory Updated");
    }
}

Step 2: Facade Class

public class OrderFacade
{
    private readonly OrderService _orderService = new();
    private readonly PaymentService _paymentService = new();
    private readonly NotificationService _notificationService = new();
    private readonly InventoryService _inventoryService = new();

    public void PlaceOrder()
    {
        _orderService.CreateOrder();
        _paymentService.ProcessPayment();
        _inventoryService.UpdateStock();
        _notificationService.SendNotification();
    }
}

Usage Example

var facade = new OrderFacade();

facade.PlaceOrder();

Output

Order Created
Payment Processed
Inventory Updated
Notification Sent

Key Concept

Instead of:

orderService.CreateOrder();
paymentService.ProcessPayment();
inventoryService.UpdateStock();
notificationService.SendNotification();

We do:

facade.PlaceOrder();

👉 One simple method hides all complexity.


Diagram Understanding

Client
   ↓
OrderFacade
   ↓
-------------------------
OrderService
PaymentService
InventoryService
NotificationService

👉 Facade acts as a central entry point.


Advantages

  • ✔ Simplifies complex systems
  • ✔ Reduces coupling
  • ✔ Improves readability
  • ✔ Easier maintenance
  • ✔ Cleaner client code

Disadvantages

  • ✖ Facade can become a God Class
  • ✖ May hide useful subsystem features
  • ✖ Additional abstraction layer

When to Use

Use Facade Pattern when:

  • Multiple services work together
  • Workflow is complex
  • You want a simple API/interface
  • System has many dependencies

Real Project Mapping (.NET + Angular)

Feature Usage
Order processing Facade
Payment workflow Facade
Email + SMS handling Facade
API orchestration Facade
Microservice aggregation Facade

ASP.NET Core Real Example

Service Layer often acts like Facade.

public class OrderApplicationService
{
    public void PlaceOrder()
    {
        // orchestrates multiple services
    }
}

👉 Controllers call one service instead of many.


Advanced Example – E-Commerce Checkout

checkoutFacade.CompleteCheckout();

Internally:

  • Validate cart
  • Process payment
  • Create invoice
  • Send email
  • Update stock

👉 Client sees only one method.


Pro Tip

Facade Pattern works well with:

  • Mediator Pattern
  • CQRS
  • Microservices
  • Clean Architecture

Summary

Facade Pattern helps you:

  • Hide system complexity
  • Provide simple APIs
  • Organize workflows cleanly

👉 Perfect for:

  • Order systems
  • Workflow orchestration
  • API aggregation
  • Service coordination

Comments

Popular posts from this blog

Promises in Angular

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

Csharp Coding - Session