none
Virual Proxy pattern RRS feed

  • Question

  • Hi All,

    I am trying to understand the proxy pattern. One of its implementation is remote proxy while other is virtual proxy(lazy loading).I am searching for a simpleexample implemtation of virual proxy in c# but couldn't find by googling.

    Can somebody please share any link where i can find virtual proxy implementation which is simple :)

    Cheers

    Thompson

    Monday, January 30, 2012 9:13 AM

Answers

  • Using an MVC (Model View Controller) pattern to decouple the Order from your data access layer  I would see the solution as follows:

    //implements an order (Model)
    public class Order:IOrder
    {
     ICustomer _customer;
     public ICustomer Customer
    {
           //returns the customer for this order
          get
         {
              return _customer;
         }
    }
    }
    //implements customer (Model)
    public class Customer:ICustomer
    {
     //
                  IController _controller;
                 public Customer ()
                  {
                      _controller = new Controller();
                  }    
    List<IOrder> _orderList;
     
                  public int ID {get;}
                 //lazy loads a list of orders placed by this customer
    public  List<IOrder> OrderList
    {
    get
    {
         if(_orderList == null)
      _orderList = _controller.GetOrdersForCustomer(this);
         return _orderList;
                  }
    }
    }

    //implements the controller component (Controller)
    public class Controller: IControler
    {
         public List<IOrder> GetOrdersForCustomer(ICustomer customer)
        {
           // makes a call to a service to get all orders for this customer
              return _serviceProxy.GetOrdersForCustomer(customer.ID);
        }

    }


    //in your UI (View)
    //when the user navigates to the list of orders …
    order.Customer.OrderList
    //the order list will be lazy loaded .


    If the list of orders for a customer can be very large than this solution would not be optimal. In this case maybe you would only return the list of order numbers and each order in the list should be lazy loaded when you access it similar to the code above.

    • Marked as answer by thompson3 Thursday, February 2, 2012 5:54 AM
    Tuesday, January 31, 2012 2:20 PM

All replies

  • A virtual proxy delays the loading of the real object until it is needed.

    This is done transparently so that the proxy consumer does not need to know about this implementation.

    This pattern is usefull when the real object is expensive to load or takes up memory.

    You could have somethink like this:

     

    public class VirtualCar : ICar

    {

         ICar _realCar = null;

         public void ICar.Drive()

        {

            if(_realCar == null)

               LoadCar();

            _realCar.Drive();

        }

        void LoadCar()

        {

           // creates the real Car object and asigns it to _realCar;

        }

    }

    Creating the real object can be implemented by using the new operator or by sophisticated methods such as dynamic loading or inversion of control.

    An example of dynamic loading can be found at

    http://www.codeproject.com/Articles/13747/Dynamically-load-a-class-and-execute-a-method-in-N

    for inversion of control you can use Castle Windsor

    http://stw.castleproject.org/Windsor.MainPage.ashx

     

     

    Monday, January 30, 2012 6:12 PM
  • Hi Razvan,

    Thanks a lot for your inputs. Can you please spare some time and provide a complete example like Icar example using new operator.

    I feel that Icar example doesn't fits the lazy loading exactly(therotically it is right but might not be for practical usage) until unless we are loading some parts lazyly which are rarely used.

    I am searching for some example like this:-  In an order there is customer object and also a list of all orders linked to a specific customer. Now this list of all orders linked to a customer will be lazy loaded if required which getting customer details of a particular order.

    It would be very helpful for me if you could provide some real life example code.

    Thanks in advance

    Cheers

    Thompson


    • Edited by thompson3 Tuesday, January 31, 2012 11:20 AM Rearticulation
    Tuesday, January 31, 2012 5:24 AM
  • Using an MVC (Model View Controller) pattern to decouple the Order from your data access layer  I would see the solution as follows:

    //implements an order (Model)
    public class Order:IOrder
    {
     ICustomer _customer;
     public ICustomer Customer
    {
           //returns the customer for this order
          get
         {
              return _customer;
         }
    }
    }
    //implements customer (Model)
    public class Customer:ICustomer
    {
     //
                  IController _controller;
                 public Customer ()
                  {
                      _controller = new Controller();
                  }    
    List<IOrder> _orderList;
     
                  public int ID {get;}
                 //lazy loads a list of orders placed by this customer
    public  List<IOrder> OrderList
    {
    get
    {
         if(_orderList == null)
      _orderList = _controller.GetOrdersForCustomer(this);
         return _orderList;
                  }
    }
    }

    //implements the controller component (Controller)
    public class Controller: IControler
    {
         public List<IOrder> GetOrdersForCustomer(ICustomer customer)
        {
           // makes a call to a service to get all orders for this customer
              return _serviceProxy.GetOrdersForCustomer(customer.ID);
        }

    }


    //in your UI (View)
    //when the user navigates to the list of orders …
    order.Customer.OrderList
    //the order list will be lazy loaded .


    If the list of orders for a customer can be very large than this solution would not be optimal. In this case maybe you would only return the list of order numbers and each order in the list should be lazy loaded when you access it similar to the code above.

    • Marked as answer by thompson3 Thursday, February 2, 2012 5:54 AM
    Tuesday, January 31, 2012 2:20 PM