Skip to main content

What, Why and How of Facade Pattern in C#

· 3 min read
Adnan Rafiq
VP of Technology

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:

Feedback​

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

Your team has features to ship. Production problems keep pulling them back.

  • Slow screens and reports frustrate users
  • The same timeouts and failures keep coming back
  • Your application struggles when demand peaks

I find and fix performance and stability problems across .NET and SQL Server, and use the causes we uncover to build AI guardrails that help catch those patterns in new code.

DM me on X with the problem taking the most time from your team. Watch my diagnostics walkthrough on On .NET Live.