?

Log in

Previous Entry | Next Entry

In simple words on demand object loading rather than loading the objects unnecessarily.

Definition: Lazy loading is a concept where we delay the loading of the object unit the point where we need it.

For example consider the below example where we have a simple “Customer” class and this “Customer” class has many “Order” objects inside it. Have a close look at the constructor of the “Customer” class. When the “Customer” object is created it also loads the “Order” object at that moment. So even if we need or do not need the address object, it’s still loaded.

But how about just loading the “Customer” object initially and then on demand basis load the “Order” object.

So let’s consider you have client code which consumes the “Customer” class as shown below. So when the “Customer” object is created no “Order” objects should be loaded at that moment. But as soon as the “foreach” loop runs you would like to load the “Order” object at that point (on demand object loading).

public class Customer
{
private List _Orders= null;


public Customer()
{
           _CustomerName = "Shiv";
           _Orders = LoadOrders(); // Loads the address object even though //not needed

}

private List LoadOrders()
{
            List temp = new List();
            Order o = new Order();
            o.OrderNumber = "ord1001";
            temp.Add(o);
            o = new Order();
            o.OrderNumber = "ord1002";
            temp.Add(o);
            return temp;
}

}
 

So let’s consider you have client code which consumes the “Customer” class as shown below. So when the “Customer” object is created no “Order” objects should be loaded at that moment. But as soon as the “foreach” loop runs you would like to load the “Order” object at that point (on demand object loading).

Customer o = new Customer(); // Address object not loaded
Console.WriteLine(o.CustomerName);
foreach (Order o1 in o.Orders) // Load address object only at this moment
{
      Console.WriteLine(o1.OrderNumber);
}

Implementation of Lazy Loading: -

So for the above example if we want to implement Lazy loading we will need to make the following changes:-

  • Remove the “Order” object loading from the constructor.

  • In the “Order” get property, load the “Order” object only if it’s not loaded.

public class Customer
{
private List _Orders= null;


public Customer()
{
           _CustomerName = "Shiv";
}

public List Orders
{
      get
        {
               if (_Orders == null)
               {
                   _Orders = LoadOrders();
               }
               return _Orders;
         }

 }
 

Now if you run the client code and halt your debugger just before the “ForEach” loop runs over the “Orders” object, you can see the “Orders” object is null (i.e. not loaded). But as soon as the “ForEach” loop runs over the “Order” object it creates the “Order” object collection.
                  cosole

Also see following .NET interview questions video on Lazy Loading Design Pattern: -

Copy right www.questpond.com

Buy 600+ Real Time Interview Videos

Call +91-22-66752917 or mail us on questpond@questpond.com for more details on C#, .NET, ASP.NET, SQL Server interview question videos.

SQL Server interview question videos

Latest Month

March 2017
S M T W T F S
   1234
567891011
12131415161718
19202122232425
262728293031 

Tags

Powered by LiveJournal.com