A Simple way to Implement Lazy Loading

What is Lazy Loading?

Lazy Loading is an important paradigm to defer initialization of an object until the point at which it is needed.

Why we need it?

For making Application to:

  • Run faster
  • Consume less memory

In this article, I’ll show how to implement Lazy Loading.

Let us create a class Company which contains two properties, CompanyName and Employees. We will initialize CompanyName in Company constructor and initialize Employees only when we need it.

  1. public class Company  
  2. {  
  3.     public string CompanyName;  
  4.     public Lazy < List < Employee >> Employees = null//1. Mark Employees property as Lazy  
  5.     public Company()  
  6.         {  
  7.             CompanyName = "MNC";  
  8.             Employees = new Lazy < List < Employee >> (() => getEmployees()); // 2. Asking the property to load values from getEmployees() method  
  9.         }  
  10.         //Method to get Employees.  
  11.     public List < Employee > getEmployees()  
  12.     {  
  13.         List < Employee > Employees = new List < Employee >  
  14.         {  
  15.             new Employee  
  16.             {  
  17.                 FirstName = "Sridhar", LastName = "Adusumilli"  
  18.             }, new Employee  
  19.             {  
  20.                 FirstName = "Manas", LastName = "Mohapatra"  
  21.             }  
  22.         };  
  23.         return Employees;  
  24.     }  
  25. }  
  26. //Employee Class which contains FirstName and LastName properties  
  27. public class Employee  
  28. {  
  29.     public string FirstName  
  30.     {  
  31.         get;  
  32.         set;  
  33.     }  
  34.     public string LastName  
  35.     {  
  36.         get;  
  37.         set;  
  38.     }  
  39. }  
Now, we will initialize Employees property by using cmp.Employees.Value.
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         Company cmp = new Company();  
  6.         Console.WriteLine(cmp.CompanyName);  
  7.         foreach(var item in cmp.Employees.Value) //3. When we call cmp.Employees.Value then only the Employee list will be populated.  
  8.             {  
  9.                 Console.WriteLine(item.FirstName + " " + item.LastName);  
  10.             }  
  11.         Console.ReadLine();  
  12.     }  
  13. }  
Complete Code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace SampleLazyLoading  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Company cmp = new Company();  
  13.             Console.WriteLine(cmp.CompanyName);  
  14.             foreach(var item in cmp.Employees.Value) //3. When we call cmp.Employees.Value then the Employee list will be populated here.  
  15.                 {  
  16.                     Console.WriteLine(item.FirstName + " " + item.LastName);  
  17.                 }  
  18.             Console.ReadLine();  
  19.         }  
  20.     }  
  21.     public class Company  
  22.     {  
  23.         public string CompanyName;  
  24.         public Lazy < List < Employee >> Employees = null//1. Mark Employees property as Lazy  
  25.         public Company()  
  26.             {  
  27.                 CompanyName = "MNC";  
  28.                 Employees = new Lazy < List < Employee >> (() => getEmployees()); // 2. Asking the property to load values from getEmployees() method  
  29.             }  
  30.             //Method to get Employees.  
  31.         public List < Employee > getEmployees()  
  32.         {  
  33.             List < Employee > Employees = new List < Employee >  
  34.             {  
  35.                 new Employee  
  36.                 {  
  37.                     FirstName = "Sridhar", LastName = "Adusumilli"  
  38.                 }, new Employee  
  39.                 {  
  40.                     FirstName = "Manas", LastName = "Mohapatra"  
  41.                 }  
  42.             };  
  43.             return Employees;  
  44.         }  
  45.     }  
  46.     //Employee Class which contains FirstName and LastName properties  
  47.     public class Employee  
  48.     {  
  49.         public string FirstName  
  50.         {  
  51.             get;  
  52.             set;  
  53.         }  
  54.         public string LastName  
  55.         {  
  56.             get;  
  57.             set;  
  58.         }  
  59.     }  
  60. }  
Conclusion

We need to check carefully whether the values pertaining to property initialization has been called otherwise the application will be at a risk of null exception.