Home  /  Questions  /  Question



56   94.9
Mar 15, 2010


Lazy Loading in Windows Forms

Hi,

I have a question: can we use the Lazy Loading pattern in Windows application?
Appreciate if someone would explain this with some examples.

Thanks
Jebby Philip



380   99.9
Mar 19, 2010
Yes, you can. Lazy loading is nothing but deferring the loading of constiuent objects (referenced objects) of a class until it is required/accessed. May be this would help: http://www.lhotka.net/Article.aspx?id=48548dc3-3b64-4bb3-bc84-9476b8f77600
 1 comment
 
You're right. It is not related to Windows or Web Programming specifically. Lazy loading is useful in any kind of environment. --- Juan Perez  Mar 19, 2010

130   96.7
Mar 20, 2010
Lazy loading is just a fancy name given to the process of intializing a class when it's actually needed. You might already be doing it.
For example suppose you are maintaining a collection of orders on the form and it's a class level variable, which you will fill thought out the life time of the form.

Since you know that you are going to use this collection you may say while declaring at form level

private List<orders> myOrders = new List<orders>();

or you may choose to lazy load it

private List<orders> myOrders;


and then in you form when you add the first item you do this

if ( myOrders == null)
     myOrders = new List<orders>();

myOrders.add (firstOrder);

330   99.9
Apr 25, 2010
Another example is when you need to access a parent object in your object graph. For example, if you have an Invoice and you want to know the Customer of this Invoice, it will be more easy to say :

Invoice.Customer.Name
so, in this case you should prepare you Invoice class to load the Customer Object in the getter property like :

private Customer _customer;

  
public Customer Customer
{
   get
   {
       if (_customer == null) 
       {
          _customer = GetCustomerById( Id );
       }
       return _customer;
   }
}
I hope this helps
 

56   94.9
Mar 24, 2010
Thanks Shiva, Juan, Syed