Skip to main content

Facade: Simplifying Interactions with Complex Subsystems

Design Patterns Explained: From Theory to Practice — Article 11

Updated 10 min readMarkdown
Part of seriesDesign Patterns

As applications grow, their internal structures become more complex. A single feature might depend on several services, each responsible for a different part of the system.

Consider a ticket-management application. When an agent opens a ticket, the application might need to retrieve the ticket details, calculate its SLA status and load its recent activity history.

Each operation has its own service and responsibility. That separation is useful, but it can create a problem for the code that needs to bring everything together.

A controller might end up coordinating several dependencies, understanding the order in which they should be called and knowing how their results fit together. As the feature grows, the controller becomes increasingly coupled to the internal structure of the application.

Could we give the controller a simpler way to retrieve everything it needs without combining all those responsibilities into one large service?

This is where the Facade pattern becomes useful.

What is the Facade pattern?

The Facade pattern is a structural design pattern that provides a simplified interface to a complex subsystem.

Rather than requiring clients to interact with multiple classes and understand how they work together, a facade offers a smaller number of high-level operations that hide the coordination details.

The underlying subsystem remains responsible for its own functionality. The facade simply provides a convenient entry point through which clients can access that functionality.

For our ticket-management application, a "TicketWorkspaceFacade" could provide one operation to retrieve everything needed to display a ticket workspace.

The controller would no longer need to know which services retrieve ticket details, calculate SLA status or load activity history. It would simply request the workspace.

The important distinction is that the Facade pattern does not simplify the subsystem itself. It simplifies how other parts of the application interact with it.

Understanding the participants

The Facade pattern has three main participants:

Participant| Responsibility

Facade| Provides a simplified interface and coordinates access to the subsystem.

Subsystem classes| Perform the underlying operations and contain the functionality required by the application.

Client| Interacts with the facade instead of managing the subsystem's individual components directly.

The subsystem classes do not need to know that a facade exists. They can continue working independently and communicating with one another where necessary.

This is an important part of the pattern: the facade introduces a convenient entry point without requiring the entire subsystem to be redesigned around it.

Implementing the Facade pattern in C#

Let's use a ticket workspace as our example.

When an agent opens a ticket, the application needs to display three things:

- The ticket's basic information.

- Its current SLA status.

- Its recent activity history.

We will assume these responsibilities already belong to separate application services. Our goal is not to combine their underlying business logic, but to provide a simpler way to access their results.

1. Identify the subsystem services

First, we define the data that the ticket workspace needs.

public sealed record TicketSummary(

Guid Id,

string Reference,

string Title,

TicketStatus Status,

TicketPriority Priority);

public sealed record TicketSlaSummary(

DateTimeOffset ResponseDeadline,

bool IsBreached);

public sealed record TicketActivityItem(

DateTimeOffset OccurredAt,

string Description);

public sealed record TicketWorkspace(

TicketSummary Ticket,

TicketSlaSummary? Sla,

IReadOnlyList<TicketActivityItem> RecentActivity);

These models represent the information that will be presented to the agent. They do not need to expose the internal implementation details of the services that retrieve them.

Next, we define the subsystem interfaces.

public interface ITicketSummaryReader

{

Task<TicketSummary?> GetAsync(

Guid ticketId,

CancellationToken cancellationToken);

}

public interface ITicketSlaReader

{

Task<TicketSlaSummary?> GetAsync(

Guid ticketId,

CancellationToken cancellationToken);

}

public interface ITicketActivityReader

{

Task<IReadOnlyList<TicketActivityItem>> GetRecentAsync(

Guid ticketId,

int count,

CancellationToken cancellationToken);

}

Each interface has a focused responsibility. The ticket summary reader retrieves ticket information, the SLA reader retrieves its SLA status and the activity reader retrieves recent history.

These services can be used independently by other parts of the application. Introducing a facade does not make them redundant.

2. Create the facade

Now we introduce "TicketWorkspaceFacade".

The facade receives the subsystem services through constructor injection and coordinates the calls required to assemble the workspace.

public sealed class TicketWorkspaceFacade

{

private readonly ITicketSummaryReader _ticketSummaryReader;

private readonly ITicketSlaReader _ticketSlaReader;

private readonly ITicketActivityReader _ticketActivityReader;

public TicketWorkspaceFacade(

ITicketSummaryReader ticketSummaryReader,

ITicketSlaReader ticketSlaReader,

ITicketActivityReader ticketActivityReader)

{

_ticketSummaryReader = ticketSummaryReader;

_ticketSlaReader = ticketSlaReader;

_ticketActivityReader = ticketActivityReader;

}

public async Task<TicketWorkspace?> GetAsync(

Guid ticketId,

CancellationToken cancellationToken)

{

var ticket = await _ticketSummaryReader.GetAsync(

ticketId,

cancellationToken);

if (ticket is null)

{

return null;

}

var sla = await _ticketSlaReader.GetAsync(

ticketId,

cancellationToken);

var recentActivity =

await _ticketActivityReader.GetRecentAsync(

ticketId,

count: 10,

cancellationToken);

return new TicketWorkspace(

ticket,

sla,

recentActivity);

}

}

Notice what the facade does and does not do.

It coordinates the subsystem services and combines their results into one response. However, it does not implement ticket retrieval, SLA calculations or activity-history retrieval itself.

Those responsibilities remain with their respective services.

The facade also handles a straightforward case: if the ticket does not exist, there is no reason to continue retrieving its SLA status or activity history.

The calls are deliberately sequential here. If these services use the same Entity Framework Core "DbContext", running their database operations concurrently could cause errors because a "DbContext" does not support multiple parallel operations. Parallel execution is an option only when the underlying dependencies support it.

3. Let the client use the facade

Now consider the API controller that provides the ticket workspace endpoint.

Without a facade, the controller would need to inject all three subsystem services and coordinate their calls itself.

With the facade, it only needs one dependency.

[ApiController]

[Route("api/tickets")]

public sealed class TicketWorkspaceController : ControllerBase

{

private readonly TicketWorkspaceFacade _workspaceFacade;

public TicketWorkspaceController(

TicketWorkspaceFacade workspaceFacade)

{

_workspaceFacade = workspaceFacade;

}

[HttpGet("{ticketId:guid}/workspace")]

public async Task<ActionResult<TicketWorkspace>> GetWorkspace(

Guid ticketId,

CancellationToken cancellationToken)

{

var workspace = await _workspaceFacade.GetAsync(

ticketId,

cancellationToken);

if (workspace is null)

{

return NotFound();

}

return Ok(workspace);

}

}

The controller no longer needs to understand how the workspace is assembled. It asks the facade for the information and handles the HTTP response.

This is the central benefit of the pattern: the client interacts with a simpler interface while the complexity remains encapsulated within the subsystem.

4. Register the dependencies in modern .NET

In an ASP.NET Core application, we can use the built-in dependency injection container to register the subsystem services and the facade.

builder.Services.AddScoped<

ITicketSummaryReader,

EfTicketSummaryReader>();

builder.Services.AddScoped<

ITicketSlaReader,

EfTicketSlaReader>();

builder.Services.AddScoped<

ITicketActivityReader,

EfTicketActivityReader>();

builder.Services.AddScoped<TicketWorkspaceFacade>();

The facade receives its dependencies automatically when ASP.NET Core creates it.

Dependency injection does not implement the Facade pattern. It simply manages the creation and lifetime of the objects involved.

The design pattern is the simplified interface and the coordination it provides. Dependency injection is the mechanism used to supply the facade with its collaborators.

When should you use the Facade pattern?

The Facade pattern is useful when clients need to interact with several parts of a subsystem to accomplish a common task.

Some practical scenarios include:

Working with complex third-party libraries

A third-party library might expose many classes and configuration options. Rather than allowing application code to depend directly on all those details, a facade can expose only the operations the application needs.

Simplifying application workflows

When a feature requires information or operations from several services, a facade can provide a single, higher-level entry point for that interaction.

Reducing coupling between application layers

Controllers and other clients can depend on a stable facade instead of knowing about every internal service involved in a particular operation.

Isolating implementation changes

If the underlying subsystem changes, the facade can absorb some of those changes. Clients may continue using the same interface, provided its behaviour remains compatible.

However, not every group of service calls needs a facade. If a client only needs one service, or the coordination is already straightforward, introducing another abstraction may not provide much value.

Advantages and trade-offs

The Facade pattern offers several advantages:

- Simpler client interactions: Clients use a smaller interface rather than coordinating multiple subsystem components.

- Reduced coupling: Clients depend on the facade instead of knowing about every internal service.

- Centralised coordination: Common workflows can be organised behind a single entry point.

- Isolation from implementation changes: Changes within the subsystem can sometimes be handled without affecting its clients.

However, there are trade-offs to consider:

- Additional code: A facade introduces another layer that needs to be maintained.

- Maintenance overhead: Changes to the subsystem's required interactions may require corresponding changes to the facade.

- Risk of excessive responsibility: A facade can become a God Object if unrelated operations are continually added to it.

- Reduced visibility: A facade may hide functionality that some clients genuinely need.

One particularly important design consideration is avoiding the God Object problem.

A facade should not become a single class through which every feature in the application must pass. Doing so simply moves complexity from the subsystem into another class.

Instead, create facades around coherent areas of functionality. A "TicketWorkspaceFacade" and a "TicketReportingFacade", for example, could serve different client needs without becoming one enormous application-wide facade.

It is also worth remembering that a facade does not automatically provide transaction management, data consistency or error recovery. In our example, the workspace is assembled through multiple reads. If the underlying data changes between those reads, the resulting response might not represent one consistent snapshot. Where that matters, the data access strategy must address it explicitly.

Facade versus other design patterns

Facade is sometimes confused with other structural patterns because they can all introduce an additional layer between a client and an existing component.

However, their intentions are different.

Pattern| Primary intention

Facade| Provides a simplified interface to a complex subsystem.

Adapter| Converts an existing interface into one that a client expects.

Decorator| Adds responsibilities to an object without changing its interface.

Mediator| Centralises communication between objects that would otherwise communicate directly.

For example, our "TicketWorkspaceFacade" does not translate an incompatible interface, as an Adapter would. It does not wrap a notification sender to add logging, as a Decorator would. Nor does it coordinate communication between a group of independent objects in the way a Mediator does.

It simply provides a convenient entry point to several existing services.

Understanding the intention behind each pattern is more useful than trying to identify patterns based on their class diagrams alone.

Conclusion

The Facade pattern is about managing complexity at the boundaries of a subsystem.

As applications grow, clients should not need to understand every internal dependency or the sequence of operations required to complete a common task.

By introducing a simplified interface, a facade allows the underlying components to retain their individual responsibilities while making them easier to use together.

However, simplicity should remain the objective. A facade that becomes another complex subsystem has defeated its own purpose.

The goal is not to hide complexity for its own sake. It is to expose the functionality clients need without forcing them to understand details they do not need.

In the next article, we will explore the Flyweight pattern, which addresses a different kind of problem: reducing memory consumption when an application needs to manage large numbers of similar objects.

Further reading

- Alexander Shvets — Dive Into Design Patterns, Structural Design Patterns: Facade.

- Vaskaran Sarcar — Design Patterns in C#, 2nd Edition, Chapter 9: Facade Pattern.

No comments yet. Be the first to comment.