Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / C#
Tip/Trick

Caching uses with Proxy pattern in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
12 Jun 2011CPOL1 min read 29.2K   6   2
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia

Comments and Discussions

 
Generalis it a service locator ? Pin
Pierre-Andr Dejoie13-Jun-11 18:21
Pierre-Andr Dejoie13-Jun-11 18:21 
GeneralRe: is it a service locator ? Pin
Mohammad A Rahman13-Jun-11 20:13
Mohammad A Rahman13-Jun-11 20:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.