Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Caching uses with Proxy pattern in C#

0.00/5 (No votes)
12 Jun 2011 1  
Caching uses with Proxy pattern in C#

Proxy pattern can be used to create a mediator to call Service functionality. So basically, a Proxy can implement one or more interfaces, and using related concrete objects, a proxy can access functionality and work as a mediator between the client and implementer classes of those interfaces.


In this post, I will describe a Generic Proxy Pattern by which I will access multiple interfaces functionality; for instance, if I have IInterfaceOne, IInterfaceTwo, and implementers as InterfaceOneImp and InterfaceTwoImp, then using the generic ProxyPattern, I can access the functionality of the implementor classes. This proxy class will maintain a data structure to store the instances of the related types called from the client.


C#
private static Lazy<Dictionary<string, object>> objectRepository = 
  new Lazy<Dictionary<string, object>>(() => 
  new Dictionary<string, object>());

When the client sends a request to the proxy by specifying the Type, then the proxy will create an instance of that type and store it into the objectRepository for later reuse. So after the first request, when the client passes another request for the same type, the proxy will re-use the instance created before to serve the request.


To illustrate the concept, I will show an example. In the example code, I have used a few interfaces; for example,


C#
public interface ICalculation
{
    void ProcessCalculation(int a, int b);
    int ProcessMultiplication(int a, int b);
}

public interface IMaintain
{
    void ProcessSystem();
}

and the implementer of those interfaces,


C#
public class CalculationImp : ICalculation
{
   private int result = new Random().Next(77777);
   public void ProcessCalculation(int a, int b)
   { Console.WriteLine(result); }
   public int ProcessMultiplication(int a, int b)
   { return a * b; }
}

public class CalculationSecondImp : ICalculation
{
   private int result = new Random().Next(55555);
   public void ProcessCalculation(int a, int b)
   { Console.WriteLine(result); }
   public int ProcessMultiplication(int a, int b)
   { return a * b * 5; }
}

public class MaintainImp : IMaintain
{
   public void ProcessSystem()
   { Console.WriteLine("Process system"); }
}

To implement the Proxy Pattern, I created two partial classes. The first one will act as a Proxy of the first interface which is ICalculation, and the second one for IMaintain. Both of those partial classes will share the same data structure to store the instances of the real object; in here CalculationImp, CalculationSecondImp, and MaintainImp. The following code will show the whole logic:


C#
public partial class ProxyPatternWrapper
{
    public static void ProcessAddition<T>(int a, int b)
        where T : ICalculation, new()
    {
        if (!objectRepository.Value.Keys.Contains(typeof(T).FullName))
        {
            objectRepository.Value.Add(typeof(T).FullName, new T());
        }
        ((T)objectRepository.Value[typeof(T).FullName]).ProcessCalculation(a, b);
    }

    public static int ProcessMultiplication<T>(int a, int b)
        where T : ICalculation, new()
    {
        if (!objectRepository.Value.Keys.Contains(typeof(T).FullName))
        {
            objectRepository.Value.Add(typeof(T).FullName, new T());
        }
        return ((T)objectRepository.Value[typeof(T).FullName]).ProcessMultiplication(a, b);
    }
}

public partial class ProxyPatternWrapper
{
    public static void ProcessSystemMaintain<T>()
        where T : IMaintain, new()
    {
        if (!objectRepository.Value.Keys.Contains(typeof(T).FullName))
        {
            objectRepository.Value.Add(typeof(T).FullName, new T());
        }
        ((T)objectRepository.Value[typeof(T).FullName]).ProcessSystem();
    }
}

The usage of the above code is below:


C#
ProxyPatternWrapper.ProcessAddition<CalculationImp>(1, 2);
ProxyPatternWrapper.ProcessAddition<CalculationSecondImp>(1, 2);
ProxyPatternWrapper.ProcessAddition<CalculationImp>(1, 2);
ProxyPatternWrapper.ProcessSystemMaintain<MaintainImp>();

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here