Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Can You Explain Lazy Loading?

4.85/5 (113 votes)
20 Jun 2021CPOL3 min read 356.1K  
About the Lazy loading concept where we delay the loading of the object until the point where we need it

 

Image 1

Introduction

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.

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 Order object, it’s still loaded.

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

C#
public class Customer
{
	private List<Order> _Orders= null;
	…
	…
	public Customer()
	{
        _CustomerName = "Shiv";
        _Orders = LoadOrders(); // Loads the order object even though //not needed
	}
     
	private List<Order> LoadOrders()
	{
        List<Order> temp = new List<Order>();
        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).

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

So How Do We Implement Lazy 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.
C#
public class Customer
{
    private List<Order> _Orders= null;
    …
    …
    public Customer()
    {
        _CustomerName = "Shiv";
    }
    public List<Order> 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.

Image 2

Are there any Readymade Objects in .NET by which We Can Implement Lazy Loading?

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.

C#
private Lazy<List<Order>> _Orders= null; 

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

C#
_Orders = new Lazy<List<Order>>(() => LoadOrders());  

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.

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

Below goes the full code for this:

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

What are the Advantages and Disadvantages of Lazy Loading?

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.

Also, have a look at the below posted video on Lazy Loading:

Image 3

For further reading, do watch the below interview preparation videos and step by step video series:

History

  • 12th September, 2013: Initial version
  • 21st June, 2021: Updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)