Facade: Implementation and Example

Implementing the Facade design pattern

Let’s imagine that we need to build an application where, if we input the account name, we’re able to retrieve the list of products this account is allowed to buy. To achieve this, we’ll have to deal with multiple back-end systems. A list of products will be returned based on which category the account belongs to and whether or not the account is a buyer or a reseller. However, our application doesn’t hold this information. There’s another service that’ll return the account category based on the account ID. The problem is that our application doesn’t store the account ID either. It operates within its own bounded context where account id doesn’t exist. Only the name of the account is known. So, we need to call another service to obtain the account ID based on the account name.

As we can see, it’s a relatively complex interaction between multiple services. This is where the Facade design pattern fits perfectly.

Creating an application

We’ll start by creating a .NET console application. Then, we’ll add a service that’ll return a list of accounts. This service would be represented by the AccountDataService class and its definition would be as follows:

Get hands-on with 1200+ tech skills courses.