Facade – A single class that represents an entire subsystem that hides the system complexity.
From Design Patterns – Structural Patterns
Structure
Applicability
Pros and Cons
PROS
CONS
How to Implement
#1 Basic Sample
class Program
{
static void Main(string[] args)
{
SubSystem1 subSystem1 = new SubSystem1();
SubSystem2 subSystem2 = new SubSystem2();
Facade facade = new Facade(subSystem1, subSystem2);
Console.WriteLine(facade.Operation());
Console.ReadKey();
}
}
public class Facade
{
protected SubSystem1 SubSystem1;
protected SubSystem2 SubSystem2;
public Facade(SubSystem1 _subSystem1, SubSystem2 _subSystem2)
{
this.SubSystem1 = _subSystem1;
this.SubSystem2 = _subSystem2;
}
public string Operation()
{
string Result = "Facade initializes subsystem:\n";
Result += this.SubSystem1.OperationSub1();
Result += this.SubSystem2.OperationSub2();
Result += "Facade orders subsystem to perform the action:\n";
Result += this.SubSystem1.OperationNSub1();
Result += this.SubSystem2.OperationNSub2();
return Result;
}
}
public class SubSystem1
{
public string OperationSub1() => $"Operation 1 from Subsystem1 \n";
public string OperationNSub1() => $"Operation N from Subsystem1 \n";
}
public class SubSystem2
{
public string OperationSub2() => $"Operation 1 from Subsystem2 \n";
public string OperationNSub2() => $"Operation N from Subsystem2 \n";
} #2 Web Application Project
Step 1 Create two services and the generic type repositories. These services will call the generic type repository’s interface object.
Step 2 Create a Facade class file, then implement two interfaces and create a new function for these interface operation processes.
Step 3 Create the Facade services in the Core project, then implement the Facade class file and create a new function for layout.
Step 3 Register the related-facade pattern class file in the Startup.cs file.
services.AddScoped(typeof(IFacade<>), typeof(FacadeRepo<>));
services.AddScoped(typeof(FacadeServices)); Step 4 Create the Controller files for the Facade pattern in the web project.
Reference
What is the API gateway? An API gateway is an API management tool that sits…
The .NET 5 SDK is a kind of milestone in the .NET world. The .NET…
In object-oriented design, the dependency inversion principle is a specific methodology for loosely coupling software…
In the field of software engineering, the interface segregation principle (ISP) states that no code…
Subtype Requirement: Let be a property provable about objects of type T. Then should be true for objects of type S where S is…
In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be…
View Comments