Click here to Skip to main content
Click here to Skip to main content

Caching uses with Proxy pattern in C#

, 12 Jun 2011
Rate this:
Please Sign up or sign in to vote.
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.

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,

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,

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:

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:

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)

About the Author

Mohammad A Rahman
Software Developer
Australia Australia
Designer and Architect.
Author of the Expert C# 5.0: with the .NET 4.5 Framework book

Comments and Discussions

 
Generalis it a service locator ? PinmemberPierre-Andr Dejoie13-Jun-11 18:21 
Hello,
First fo all, thanks for your contribution.
I wonder what is the difference between your idea and a classic service locator (like Unity).
Can you explain why did you designed a such already existing feature ?
Thanks.
GeneralRe: is it a service locator ? PinmemberMohammad A Rahman13-Jun-11 20:13 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140718.1 | Last Updated 12 Jun 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid