4 Ways to Implement Lazy Loading

4 Ways to Implement Lazy Loading

In today’s era the application development has become more modernized. Client not only checks the application flow but he/she is also much more concerned about code optimization, code cleanness, algorithms used, code re-usability, coding techniques, memory optimization and a lot more which are available in latest technologies. In modern technology, when it comes to ASP.NET Developments, we deal with mostly LINQ TO SQL Class or Entity Framework. There are lots of new features available with the above technologies; out of which one is lazy loading. In this article we will cover four ways to implement lazy loading in websites and web applications.

Concept

Lazy loading is not a new concept – it is also available in C#. In traditional ways of C# we call it late binding. Lazy loading is a very important concept in the programming world. It helps to improve performance and adapt best practices in application design. Let’s discuss why lazy loading is useful and how it helps to develop a high performance application.

Lazy loading is essential to implement when the cost of object creation is very high and the use of the object is very rare. The fundamental idea of lazy loading is to load object/data when needed.

1. Late Binding

First, we will implement a traditional concept of loading (it’s not lazy loading) and we will try to understand the problem in this. Later we will implement lazy loading to solve the problem.

Have a look at the following code. ​

lazy loading

In the above example, it is not lazy loading since we are seeing that the LoadDetail property is being populated at the time of PersonalLoan object creation. If object creation of LoanDetail is very costly then it will be a very time and resource intensive operation to create an object of a PersonalLoad class.

lazy loading

Somehow we will implement a mechanism to populate the LoadDetail property in delay, I mean if needed then we will populate the property, otherwise not.

It can solve our problem and obviously it will improve performance in the application. Have a look at the following example. We can see that the LoanDetail property will not be populated when an object of the PersonalLoan class is created.

Have a look at the following code.

lazy loading So, we are seeing that the LoanDetail Property is still null. As soon as we load the property in our code, it will be populated.

lazy loading

2. Lazy Loading using Lazy<T> Class

There are many choices to implement lazy initialization. We can use our own implementation to delay object population or we can use the Lazy<T> class of the .NET library to do it.

To prepare for lazy initialization, you create an instance of Lazy<T>. The type argument of the Lazy<T> object that you create specifies the type of the object that you want to initialize lazily. The constructor that you use to create the Lazy<T> object determines the characteristics of the initialization. Lazy initialization occurs the first time the Lazy<T>.Value property is accessed.

The Lazy<T> class contains two properties by which we can detect the status of the lazy class.

IsValueCreated This property will tell us whether or not the value is initializing in a lazy class.

ValueIt gets the lazy initialized value of the current Lazy<T> instance.

We will now implement one simple class and we will see how lazy<T> works with it. Have a look at the following example.

Have a look at the following code.

lazy loading

The Test class has been declared and the instance (lazy) has created a Lazy<T> class. We are then checking whether the value is populated or not in the Test class. In the output we are seeing the value is “False” so, the value is still not populated. Whenever the line “Test t = lazy.Value” executes, the value will be populated in the Test class and this is how the Test class will initialize lazily.​ In the next line we are accessing the property of the test class that will return a string array and we are printing it. Here is sample output in the following.

lazy loading

3. Lazy Loading in Entity Framework

Lazy loading is enabled in the Entity Framework ORM too, it is on by default in Entity Framework, so if you want to enable lazy loading in Entity Framework, you don’t need to do anything. And the opposite of lazy loading is eager loading that we will see in this example.

lazy loading

Have a look at the following code

lazy loading

We see that for “Timken eng” there are 2 in the location count. As we discussed, lazy loading is enabled by default. Let’s now explain the output. Here the total 2 query is fired in the DB. The first query is at the time of company loading and the next one is at the time of count loading within the foreach loop and the output is here.

lazy loading

Now, we will disable lazy loading in the application and we will check the behaviour of the application. Here is the code for turning off lazy loading that was enabled by default:

4. Eager loading

Have a look at the following code.

lazy loading

And we see that the number of counts for “Timken Eng” is 0 in this case. Because only one query was fired and the query within the foreach was not executed, so the count value is 0.

lazy loading

Have a look at the following code. ​​

lazy loading

We have now specified a table name “company_location” in the query within Include(), it implies that the table is also loaded at the time of query execution. If we run the code above, we will see that the count property is populated again.