Facade Design Pattern in .NET

The Facade pattern provides a simplified, unified interface to a set of complex subsystems. It hides internal complexities and makes the system easier to use for clients.

 

Purpose

To reduce complexity for the client by exposing a single point of interaction with multiple dependent classes and operations behind the scenes.

 

Key Characteristics

  • Provides a high-level API
  • Internally coordinates multiple complex operations
  • Does not restrict access to lower-level subsystems (optional)

 

Pros

  • Simplifies usage of complex systems
  • Promotes separation of concerns
  • Makes code easier to read and maintain

 

Cons

  • Can become a god class if too much is added
  • May hide important details needed by advanced users

 

Use Cases

  • Wrapping financial transactions involving multiple steps (validation, logging, processing)
  • Providing a simplified API for investment operations
  • Creating a one-call method to perform complex loan eligibility checks

 

Real-Time Finance Example: Unified Payment Processing

Suppose your system handles payment validation, balance checks, and transaction recording. Instead of letting the client deal with each class, you expose a single method via a PaymentFacade.

 

1. Subsystems

public class Validator

{

    public bool ValidateCard(string cardNumber)

    {

        Console.WriteLine("Validating card...");

        return !string.IsNullOrEmpty(cardNumber);

    }

}

 

public class BalanceChecker

{

    public bool HasSufficientFunds(string accountNumber, decimal amount)

    {

        Console.WriteLine("Checking balance...");

        return amount <= 10000; // Simulated check

    }

}

 

public class TransactionLogger

{

    public void Log(string message)

    {

        Console.WriteLine($"Transaction Log: {message}");

    }

}

 

2. Facade

public class PaymentFacade

{

    private readonly Validator _validator;

    private readonly BalanceChecker _balanceChecker;

    private readonly TransactionLogger _logger;

 

    public PaymentFacade()

    {

        _validator = new Validator();

        _balanceChecker = new BalanceChecker();

        _logger = new TransactionLogger();

    }

 

    public void MakePayment(string cardNumber, string accountNumber, decimal amount)

    {

        if (!_validator.ValidateCard(cardNumber))

        {

            Console.WriteLine("Invalid card.");

            return;

        }

 

        if (!_balanceChecker.HasSufficientFunds(accountNumber, amount))

        {

            Console.WriteLine("Insufficient balance.");

            return;

        }

 

        Console.WriteLine($"Processing payment of ₹{amount}");

        _logger.Log($"Payment of ₹{amount} from account {accountNumber}");

    }

}

 

3. Client Code Usage

class Program

{

    static void Main()

    {

        var paymentFacade = new PaymentFacade();

 

        string cardNumber = "1234-5678-9012";

        string accountNumber = "AC001";

        decimal amount = 2500;

 

        paymentFacade.MakePayment(cardNumber, accountNumber, amount);

    }

}

 

Output

Validating card...

Checking balance...

Processing payment of ₹2500

Transaction Log: Payment of ₹2500 from account AC001

 

Summary

  • The Facade Pattern provides a clean API for interacting with complex financial subsystems
  • Useful in banking, payments, KYC, investment, and tax platforms
  • Promotes a modular architecture while keeping usage simple for clients

Comments

Popular posts from this blog

Multiline to singleline IN C# - CODING

EF Core interview questions for beginners

EF Core interview questions for experienced