Click below for Step by Step and Interview Q & A (dotnetinter) wrote,
Click below for Step by Step and Interview Q & A
dotnetinter

.NET interview questions: - Can you explain Lazy Loading? How to implement “Lazy Loading”?

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: -

Tags: .net, .net interview questions, csharp interview questions, design pattern, it interview questions with answers, lazy loading, programming interview questions
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments