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
You must Login to comment.
|
Fri, Jun 27, 2008 3:53 AM
by Happy_Learner
|
Then when we can use this pattern? What about Hierarchial data loading usign this pattern?
|
|
Mon, Aug 4, 2008 7:47 AM
by Guffa
|
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.
|
Revision #31
Tue, Aug 26, 2008 6:53 PM
by
|
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
|
Revision #2
Mon, Feb 4, 2008 8:00 AM
by
|
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
|
Revision #4
Wed, Jun 11, 2008 8:33 PM
by
|
MVP Pattern
MVP (Model View Presenter)Framework The MVP pattern is an Architecture Pattern used to build ASP.net applications. It refers to splitting up the responsibilities for gathering, displaying, and storing data from a web page into separate objects: a Model object
|
Revision #6
Mon, Feb 4, 2008 8:00 AM
by
|
Security
ASP.NET security is a huge topic and we're only scratching the surface. Let's continue to categorize in the add new content around security to make thisa greatresource.This isjust the overview page, make sure to visit thesubpages fromthe Table ofContents
|