Tuesday, 29 March 2016

Lazy Loading

Lazy loading is a concept where we delay the loading of the object until the point where we need it. Putting in simple words, on demand object loading rather than loading objects unnecessarily.

public class Customer{
         private string _MyCustomerName; 
         private List<Order> _OrdersList= null;

         public Customer() {


        _MyCustomerName = "Ghazi";  

 _OrdersList = LoadOrders(); // Loads the order object even though //not needed     

    }
 
 private List<Order> LoadOrders() {


List<Order> lstOrders = new List<Order>();

Order objOrder = new Order();
objOrder.OrderNumber = "ord101"; 
lstOrders.Add(objOrder);
objOrder = new Order();
objOrder.OrderNumber = "ord102";
lstOrders.Add(objOrder);
return lstOrders;

   }

Customer objCustomer = new Customer(); // order object not loaded

Console.WriteLine(objCustomer.MyCustomerName);

foreach (Order ord in ord.Orders) // Load order object only at this moment
{
    Console.WriteLine(ord.OrderNumber);
}

So how do we implement lazy loading?

public class Customer
{   
 private List<Order> _Orders= null;    public Customer() { 
_CustomerName = "Ghazi";    
}   
 public List<Order> Orders    { 
            get        {    
  if (_Orders == null)  { 
 _Orders = LoadOrders();
   } 
return _Orders;
  }
}

Are there any readymade objects in .NET by which we can implement lazy loading?
Yes. There is. Following piece of code I'll explain this concept.


 private Lazy<List<Order>> _Orders = null;
_Orders = new Lazy<List<Order>>(() => LoadOrders());

public List<Order> Orders{
    get    {
        return _Orders.Value;
         }
 
}

public class Customers
    {
        private Lazy<List<Order>> _Orders = null;
        public string _CustomerName;
        public List<Order> Orders
        {
            get
            {
                return _Orders.Value;
            }
        }
        public Customers()
        {
            // Makes a database trip
            _CustomerName = "Ghazi";
            _Orders = new Lazy<List<Order>>(() => LoadOrders());
        }

        private List<Order> LoadOrders()
        {

            List<Order> lstOrders = new List<Order>();
            Order objOrder = new Order();
            objOrder.OrderNumber = "ord101";
            lstOrders.Add(objOrder);
            objOrder = new Order();
            objOrder.OrderNumber = "ord102";
            lstOrders.Add(objOrder);
            return lstOrders;

        }


    }

What are the advantages and disadvantages of lazy loading?

For example, consider the above 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 Order 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 above. 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).

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.

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.
In .NET we have the Lazy<T> class which provides automatic support for lazy loading. So let’s say if you want to implement Lazy<> in the above code, we need to implement two steps:

Create the object of orders using the Lazy generic class.

Attach this Lazy<> object with the method which will help us load the order’s data.

Now as soon as any client makes a call to the _Orders object, it will call the LoadOrders function to load the data.

You will get the List<orders> data in the Value property.

Below are the advantages of lazy loading:
· Minimizes start up time of the application.
· Application consumes less memory because of on-demand loading.
· Unnecessary database SQL execution is avoided.

The only one disadvantage is that the code becomes complicated. As we need to do checks if the loading is needed or not, there is a slight decrease in performance. But the advantages are far more than the disadvantages.

FYI: The opposite of Lazy Loading is Eager loading. So in eager loading we load all the objects in memory as soon as the object is created.




(Practice Makes a Man Perfect)

No comments: