posts - 64,  comments - 183,  trackbacks - 5

The Lazy Load Pattern is a very simple and useful pattern. This pattern is usually used when a property of an object is not always accessed during the usage of the object instance. For example, image that you have a Customer object. This object has a property that contains a collection of all orders placed by the customer. In the UI you have a general view of the customer object where you only show it name and surname. For this view of the customer object you don't use the Ordes property, so it can be loaded only when neccesary, for example in a detailed view of the customer object.

LazyLoadPattern

In a classical implementation of the Customer class, the Orders property use to be as follow, and is filled in the business layer when the instance of the Customer class is created.

class Customer
{
    
#region Private members

    
private int m_id;
    private string 
m_name;
    private string 
m_surname;
    private 
OrdersCollection m_orders;

    #endregion

    #region
 Public properties

    
public int Id
    {
        
get return m_id}
        
set { m_id = value; }
    }

    
public string Name
    {
        
get return m_name}
        
set { m_name = value; }
    }

    
public string Surname
    {
        
get return m_surname}
        
set { m_surname = value; }
    }

    
public OrdersCollection Orders
    {
        
get return m_orders}
        
set { m_orders = value; }
    }

    
#endregion
}

A better performace approach is to defer the loading of the Orders property to the moment that it's accessed. If the property is accessed and the private member is null means that the property has not been initialized so you can load it value in that moment.

private OrdersCollection m_orders;

public 
OrdersCollection Orders
{
    
get
    
{
        
if (m_orders == null)
        {
            m_orders 
= new OrdersCollection(this.m_id);
        
}

        
return m_orders;
    
}
    
set
    
{
        m_orders 
= value;
    
}
}

Another approach is to convert the property Orders of the Customer class into a method called GetCustomers that loads the orders that belongs to the customer. This approach is very similar but since the Customer class is an entity it shouldn't have methods, only just properties.

public OrdersCollection GetOrders()
{
    
return new OrdersCollection(this.m_id);
}

As I said before this is a very simple and useful pattern and it's easy to implement. I tried to explain that pattern clearly but I know my english is not perfect. In the case you have doubts about what is the Lazy Load Pattern or the benefits that you can obtain with it usage check the next resources:

http://www.martinfowler.com/eaaCatalog/lazyLoad.html
http://en.wikipedia.org/wiki/Lazy_loading

Another good resource is the Lazy Load Property Snippet by Michal Talaga in his Vault of Thoughts - .NET Blog that makes easy to use this pattern in our code.

posted on Friday, June 15, 2007 1:23 AM

Post a new comment about this topic
Title  
Name  
Url

Comments   
Protected by Clearscreen.SharpHIPEnter the code you see: