Using The Proxy Pattern In C#

Introduction

 
In today’s article we will look at the Proxy pattern. We will look at what this pattern is, the advantages of using this pattern, when it should and should not be used and finally, we will look at a sample implementation of this pattern in a C# application.

The Proxy Pattern

 
The Proxy pattern is used to provide an interface that is customized for the consumer. In many cases we do not want to expose all properties and methods to the final consumer. Hence, in this case we cannot expose the main class itself. In these cases, we can create an intermediate class or proxy class which will only expose the properties and methods that are required by the final consumer. It might not be required where we need to expose all properties and methods of the final class.
 

A simple implementation of the Proxy Pattern

 
We will now look at a simple implementation of the proxy class.This application has been created using the Visual Studio 2019 Community Edition, which is a free version.

We create a new console application in .NET Core 3.1, as below,
 
Using The Proxy Pattern In C#
 
Using The Proxy Pattern In C#
 
Using The Proxy Pattern In C#
 
The final solution looks like this,
 
Using The Proxy Pattern In C#
 
The complete code is as below,
  1. // Program.cs  
  2. using System;  
  3. namespace Console AppProxyPattern {  
  4.     class Program {  
  5.         static void Main(string[] args) {  
  6.             // Create proxy  
  7.             var employeeInfoproxy = newEmployeeInfoProxy();  
  8.             Console.WriteLine("The Salary is " + employeeInfoproxy.getSalaryDetails(100));  
  9.             Console.ReadKey();  
  10.         }  
  11.     }  
  12. }  
  13. // IEmployeeInfo.cs  
  14. namespace ConsoleAppProxyPattern {  
  15.     public interface IEmployeeInfo {  
  16.         double getSalary(intemployeeId);  
  17.         double getTax(intemployeeId);  
  18.     }  
  19. }  
  20. // EmployeeInfo.cs  
  21. namespace ConsoleAppProxyPattern {  
  22.     public class EmployeeInfo: IEmployeeInfo {  
  23.         public double getSalary(intemployeeId) {  
  24.             // Make a call to a storage (e.g. DB) and get the salary amount  
  25.             return 2000.00;  
  26.         }  
  27.         public double getTax(intemployeeId) {  
  28.             // Make a call to a storage (e.g. DB) and get the salary amount  
  29.             var salary = 2000.00;  
  30.             var tax = salary * 0.05;  
  31.             return tax;  
  32.         }  
  33.     }  
  34. }  
  35. // EmployeeInfoProxy.cs  
  36. namespace ConsoleAppProxyPattern {  
  37.     public class EmployeeInfoProxy {  
  38.         private Employee InfoempInfo = newEmployeeInfo();  
  39.         public double getSalaryDetails(intemployeeId) {  
  40.             return empInfo.getSalary(employeeId);  
  41.         }  
  42.     }  
  43. }  
In the above code, we see that we have created a proxy class for the employee info class and then we only expose the get salary details method and not the get tax method. We could also do some further data cleaning etc. in the proxy class. Hence, when the final consumer only needs the salary details and we do not want to expose the tax details, we can use this proxy class to give access to that final consumer. We do not need to provide access to the main employee info class.
 
When we run the application, we get the below,
 
Using The Proxy Pattern In C#

Summary

 
In this article, we looked at the proxy pattern and what are the advantages of using this pattern. The example given here is a simple one, but the main idea was to give an outline of how this pattern is to be implemented. Happy Coding!