EP 50 : Facade Design Pattern in C#
This newsletter represents the third segment in our design patterns series, where we’ve previously explored the Decorator and Adapter design patterns.
What is Facade Design Pattern ?
Facade is structural design pattern which simplifies complex systems ( classes or libraries) to simple solution using interface.
Example
Recently while working with .NET Web APIs I came up with a situation where I had multiple classes.
RolesService class with following methods :
- CreateRoleAsync
- UpdateRoleAsync
- DeleteRoleAsync
- GetRoleByIdAsync
- GetRolesListAsync
Above class has 10 more methods, on similar fashion I had UserService, CurrenciesService and AccountsService with 10+ methods in each class.
I was only interested in RoleIds, CurrencyIds, AccountIds and UserIds.
So instead of creating instance for each service and calling needed methods, I created a FacadeClass named MasterService and injected services in its constructor.
Then I added an interface with 4 methods (GetRoleIds, GetAccountIds, GetCurrencyIds, GetUserIds) which I was looking.
MasterService implemented that interface IMasterService.