The Facade Design Pattern Example in C#

Facade design pattern is one of the most commonly used patterns. what this pattern provide is : a single interface (or facade) that encapsulates functionality from another interfaces (or classes).

I’ve written a very easy to understand example of this, and for you as a developer is very relate-able. the code below is a simple way of how to handle the Login (I bet you must ‘ve done login before !), so we will provide the application with one interface to call to get the user logged-in, and this interface (or again facade) will handle all operations that are involved in the login process from the different other classes. Enjoy!

//create a user class

public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }

}

//create the Ad authenticator class

public class Authenticator
{

public bool IsUserPartOfActiveDirectory(User _user)
{

//check User credentials against AD or whatever Auth provider
Console.WriteLine(“Authenticating User against Adctive Directory…”);

return true;

}

}

//create the roles checker class

public class UserRolesChecker
{

public bool DoesUserHaveAccessToApplication(User _user)
{
//check user against roles in Db or config file
Console.WriteLine(“Checking User Roles…”);
return true;

}

}

//the login db updater class

public class UserRecordUpdater
{
public void UpdateUserlastLoginTime(User _user)
{

Console.WriteLine(“Updating User Record in db..”);

}

}

//create the main facade class

//this class is going to be the Facade for all other participants in the Login Process
//So only one method for Login needs to be expposed from this calss , and this method
//should take care of calling all other necessary participant methods to login the user
public class LoginFacade
{
Authenticator _authenticator = new Authenticator();
UserRolesChecker _roleChecker = new UserRolesChecker();
UserRecordUpdater _dbUpdater = new UserRecordUpdater();

public bool LoginUser(User _user)
{
if (_authenticator.IsUserPartOfActiveDirectory(_user))
{

if (_roleChecker.DoesUserHaveAccessToApplication(_user))
{

_dbUpdater.UpdateUserlastLoginTime(_user);

return true;
}
else
return false;

}
else
{
return false;
}

}

}

//test the pattern in a console app

class Program
{
static void Main(string[] args)
{

Console.WriteLine(” ***********************Login Handler*******************\n\n\n”);
//initialize a user
User _user = new User();
_user.UserId = 1;
_user.UserName = “Allan”;
_user.Password = “123456”;

//so here you only call one facade to take care of all operations! isn’t that neat

LoginFacade _loginFacade = new LoginFacade();
if (_loginFacade.LoginUser(_user))
Console.WriteLine(“\n\nUser Logged-in successfully !”);
else
Console.WriteLine(“User doe not have the necessary rights/roles to access the system”);

Console.ReadLine();

}
}

//hope you enjoyed it, please note that I have included three steps in the user login process above ( Active Directory checker, Roles Checker and Db Updater), this is not mandatory, it all depends on your organization needs and whats on the ground!

Leave a comment