Proxy – An object representing another object, allowing do something from the original object.
From Design Pattern – Structural Patterns
Structure

Applicability
- Virtual proxy(lazy initialization). The program has a heavyweight service object that wastes system resources by always being up, even though the program only needs it from time to time.
- Protection proxy(access control). When the program wants only specific clients to use the service objects, in actuality, when the project’s objects are an operating system, crucial parts or the clients are various launched applications that include malicious ones.
- Remote proxy(A remote service local execution). When the program has the service objects, the service object is located on a remote server.
- Logging proxy(Logging requests). It is when the program wants to keep a request history to the service object.
- Caching proxy(Caching request results). The program needs to cache the client request results and manage the cache life cycle, especially if requests are significant.
- Smart interface. It is when the program needs to dismiss a heavyweight object once there are no clients to use it.
Pros and Cons
PROS
- Control the service objects without clients knowing about it.
- Manage the lifecycle of the service object when the clients don’t care about it.
- If the service objects aren’t ready or available, the proxy will work even.
- Open-Closed Principle. You can introduce new proxies without changing the services or clients.
CONS
- The code will become more complicated since the program needs to introduce a lot of new classes.
- The response from the service might get delayed.
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