Proxy – An object representing another object, allowing do something from the original object.
From Design Pattern – Structural Patterns
Structure
Applicability
Pros and Cons
PROS
CONS
How to Implement
#1 Basic Sample
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Client: Executing the client code with a real subject:");
RealSubject realSubject = new RealSubject();
realSubject.Request();
Console.WriteLine("------------------------------------------------------");
Console.WriteLine("Client: Executing the same client code with a proxy:");
Proxy proxy = new Proxy(realSubject);
proxy.Request();
Console.ReadKey();
}
}
public interface ISubject
{
void Request();
}
class RealSubject : ISubject
{
public void Request()
{
Console.WriteLine("RealSubject: Handling request. ");
}
}
class Proxy: ISubject
{
private RealSubject _realSubject;
public Proxy(RealSubject realSubject)
{
this._realSubject = realSubject;
}
public void Request()
{
if (this.CheckAccess())
{
this._realSubject.Request();
this.LogAccess();
}
}
public bool CheckAccess()
{
Console.WriteLine("Proxy: Checking access prior to firing a real request.");
return true;
}
public void LogAccess()
{
Console.WriteLine("Proxy: Logging the time of request. ");
}
} #2 Web Application Project
Step 1 Create the interface class about the proxy patterns in the Core project. This class file name sets “IProxy.”
Step 2 Create the proxy class file, then inherits the IProxy class file in the Core project.
Step 3 Create the request class file and the log message class file. The request class file inherits the account class files in the Core project.
Step 4 The proxy file statements the request class of the list, the message class of the list, and the IGenericTypeRepository class file, then adding the business logic in the Core project.
Step 5 Create the service class file for the proxy pattern in the Core project.
Step 6 Create the Controller files and the View files in the web project. Proxy service class file doesn’t need to register in the web project because the proxy services use the IGenericTypeRepository class file that previous articles have registered.
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…