Lazy Loading

 Rate It (0)

Lazy Loading is an object relational pattern which is used to defer the inialization of an object until its needed. The object will not contain all of the data, but it knows how to get all of them when they are needed.

The object to be lazily loaded is originally set to null, and every request for the object checks for null and creates it "on the fly" before returning it first,

There are four main ways you can implement Lazy Load: lazy initialization, virtual proxy, value holder, and ghost.

This is an example on how to apply lazy Initialization in your class.

public class Order
{
private int _OrderId = 0;
private int OrderId
{
set { _OrderId = value; }
get { return _OrderId; }
}

private Customer _Customer = null;
public Customer Customer
{
get
{
if (_Customer == null)
_Customer = Customer.GetCustomerByOrderId(_OrderId); // Lazy loading the Customer
return _Customer;
}
set { _Customer = value; }
}
}

 In this way, the first access to the Customer property will causes the customer object to be loaded.

Note : It's not always a good practice to use lazy loading, especially if loading the object requires a lot of time and resources, like accessing a webservice. In that case it's better to deep load all the required data in the object.

 

Revision number 3, Tuesday, August 26, 2008 6:54:05 PM by vik20000in

Comments

Then when we can use this pattern? What about Hierarchial data loading usign this pattern?

You can use this pattern whenever you have some data that is't always used, and that is expensive to load or create. It works fine for hierarchial data if you initally only use a minor part of the hierarchy. If you are using most of the hierarchy you should consider loading all the data at once, as it's often cheaper to load a bunch of items at once instead of loading them one by one.

Hi, It seems that lazy loading pattern is the same with factory method in design pattern. Can you site a difference between them? and give detailed example. Its interesting.

Nice Article..I used this pattern with RSSToolkit and Ajax Tabpanel. I was fetching news from different sites and displaying them in different tabs..but problem was it was fetching all the data at the same time..so at that time it wasn't efficient but it's useful as mentioned in note of this article..and of course it's different from factory pattern as it initialize the object when required.

Related Articles

Design Patterns

Design Patterns Design patterns are recognized solutions to common problems defined originally by the Gang of Four programmers. Design patterns are used throughout the ASP.NET Framework. The various patterns are commonly divided into several different groups

Prototype Pattern

The prototype is built upon the use of object cloning. The prototype creates new objects by cloning one of its concrete classes. The prototype is used in the following situations: You need to hide the concrete product classes from the client. You want to reduce

Threat Modeling

It's absolutely necessary if you're serious about security. Whitepapers/Books/Blogs Threat Modeling for ASP.NET (PDF) - an excellent white paper from Rüdiger Grimm and Henrik Eichstädt from the University of Kent Threat Modeling book from MSPress

IDisposable Pattern

The IDisposable pattern isn't one of the a classic patterns. It's a pattern suggested in MSDN to implement the IDisposable interface. You should be familiar with the pattern or with the interface because it's a basic thing to know about the .Net

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (14)
  2. codehard (3)
  3. Babunareshnarra (2)
  4. Dungimon (1)
  5. cabhilash (1)
Microsoft Communities