Lazy Loading In C# 4.0 (Object On Demand)

Object on Demand is also called Lazy loading pattern, Lazy loading delays the initialization of object. This is a new feature of C# 4.0 and can be used when we are working with large objects when it is not in use. This article will explain you about "Lazy" class.

Suppose we have Candidate class and EducationProfile class. One candidate can have more than one EducationProfile (like: Bachelors (BBA), Master(MBA)). If you want to show the EducationProfile with the respective Candidate, you need to load EducationProfiles associated with that Candidate. If you are loading an Education Profile with the respective candidate you need to initialize a Candidate object and that is supposed to be huge .

For avoiding the situation you can use the Lazy Loading Pattern. Loading of EdutioanProfile will only happen when you will use EducationProfile list. And this will make sure fast action in comparison to the normal one and performance will also increase.

Explaining an example of Candidate and EducationProfile relationship by Lazy loading:

First we need to create classes of Candidate and EducationProfile.

  1. public class EducationProfile    
  2. {    
  3.     public int Id { getset; }    
  4.     public string Digree { getset; }    
  5.     public DateTime PassingYear { getset; }    
  6. }    
  7.     
  8. //Hope this will give you a little reference of lazy loading     
  9.     
  10. public class Candiate    
  11. {    
  12.     public string Name { getset; }    
  13.     public int EducationProfileId { getset; }    
  14.     public List GetAllEducationProfile()    
  15.     {    
  16.         return educationProileList.value;    
  17.     }    
  18.     
  19.     Lazy<list<educationproile>> educationProileList;    
  20.     
  21.     public Candiate(string name, int id)    
  22.     {    
  23.         //Initializing Candiate Object    
  24.         Name = name;    
  25.         EducationProfileId = id;    
  26.         educationProileList = new Lazy<list<educationproile>>(() => { return GetEducationProfileList(id); });    
  27.         //Initialization done    
  28.     }    
  29.     
  30.     private List GetEducationProfileList(int id)    
  31.     {    
  32.         //Loading EducationProiles    
  33.         List list = new List();    
  34.         Parallel.For(100, 110, (int i) =>    
  35.         {    
  36.             EducationProile educationprofile = new EducationProile();    
  37.             educationprofile.Id = i;    
  38.             list.Add(educationprofile);    
  39.         });    
  40.         return list;    
  41.     }    
  42. }    
  43. </list<educationproile></list<educationproile>  
In the constructor of Candidate class, properties are initializing and declaring educationProileList object, which is the generic List of educationProile and filled by GetEducationProfileList method. This method will only call when educationProileList object will be used. The following is the main method which shows the behavior of lazy loading:
  1. public static void Main(string[] args)     
  2. {    
  3.     Candidate candidate = new Candidate("AnyName", 1);    
  4.     foreach(EducationProile eduProfile in candiate.GetAllEducationProfile())    
  5.     // it will actually load accounts, ie. lazy loading    
  6.     Console.WriteLine("Id:{0}",eduProfile.Id);    
  7.     Console.Read();    
  8. }   

 


Similar Articles