Skip to main content

What, Why and How of Facade Pattern in C#

· 3 min read
Adnan Rafiq

What is Facade Pattern?

Exposing the simplistic interface of anything complex is a Facade. For example, when you place an order for Pizza delivery, the application hides the complex process behind Pizza Delivery to your door.

A successful Pizza delivery order requires multiple services to participate. Facade Pattern exposes the participating services such as Store Availability, Delivery Staff Availability, inventory of items, and others via a straightforward interface. With a straightforward interface, it achieves the following objectives:

  • It reduces the indirect coupling and eliminates direct coupling between the Consumer of the Facade and participating services.
  • It hides and simplifies the interaction between the Consumer and the Facade.
  • It allows you to replace the sub-system without impacting the Consumer.

The facade pattern is similar to Adapter Pattern, but it hides sub-systems from its Consumer while the Adapter pattern focuses on the object or one sub-system.

When to use Facade Pattern?

You should refactor towards Facade Pattern when:

  • Interaction between the consumer class and sub-systems is getting complex and straightforward, and a stable interface can provide the most functionality of complex sub-systems.
  • Replacing Legacy old sub-system(s).

How to implement it?

The Facade Pattern is a container class that depends upon two or more classes (sub-systems) to complete the transaction it exposes via its interface to the consumer class. A skeleton of example class is below:

A skelton of Facade Pattern
//A facade which depends upon two services
class OrderServiceFacade
private readonly DeliveryRiderAvailability _riderAvailability;
private readonly PaymentProcessor _paymentProcessor;

//Dependcies are injected using DI Container
public OrderServiceFacade(DeliveryRiderAvailability riderAvailability, PaymentProcessor paymentProcessor)
_riderAvailability = riderAvailability;
_paymentProcessor = paymentProcessor;
public Task PlaceOrder(Order order)
//Process Payment
//Find rider
//Other steps via other sevices.
//Handle errors of participating services more gracefully
throw new NotImplementedException();

Structure of Facade Pattern

The structure of Facade patter in form of class diagram is below:

classDiagram direction TB class Client{ -Client(OrderServiceFacade orderServiceFacade) -PlaceOrder(Order order) } class OrderServiceFacade{ -OrderServiceFacade(DeliveryRiderAvailability riderAvailability, PaymentProcessor paymentProcessor) +PlaceOrder(Order order) } class DeliveryRiderAvailability{ +FindRider(Address address) Rider } class PaymentProcessor{ +ProcessPayment(CreditCard creditCard) PaymentStatus } Client ..> OrderServiceFacade OrderServiceFacade ..> DeliveryRiderAvailability OrderServiceFacade ..> PaymentProcessor


I would love to hear your feedback; feel free to share it on Twitter.