Lazy Loading with Generic Methods

Introduction

 
Let’s develop a simple C# console application to perform lazy loading with generic method.
  
Getting Started
 
In ASP.NET 5 we can create console applications. To create a new console application, we first open Visual Studio 2015. Create it using File-> New-> Project.
 
image1 
 
Now we will select the ASP.NET 5 Console Application because we will create the console application and click on the OK button.
 
image2 
 
We need to include the following references in our application.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq; 
In the main method, I have created 2 classes and 1 method
  1. EducationProfile<T> – To define class members.
  2. Candiate<T> = Initialize lazy loading in the class.
  3. CallLazyLoading = call the function
Please refer to the code snippet below
  1. static void Main(string[] args)  
  2. {  
  3.     try  
  4.     {  
  5.         CallLazyLoading();  
  6.     }  
  7.     catch (Exception Ex)  
  8.     {  
  9.         Console.WriteLine("Error:" + Ex.Message);  
  10.     }  
  11.     Console.ReadKey();  
  12. }

Initilaze the Class

 
The first-class definition for the EducationProfile<T> is given below
  1. public class EducationProfile<T>  
  2. {  
  3.     public T Id { getset; }  
  4.     public T Class { getset; }  
  5.     public T PassingYear { getset; }  
  6. } 
The second-class definition for the Candiate<T> is given below
  1. public class Candiate < T > {  
  2.  public T Name {  
  3.   get;  
  4.   set;  
  5.  }  
  6.  public T EducationProfileId {  
  7.   get;  
  8.   set;  
  9.  }  
  10.  public Lazy < List < EducationProfile < string >>> educationProfilesList = new Lazy < List < EducationProfile < string >>> ();  
  11.   
  12.  public Candiate(T name, T id) {  
  13.   //Initializing Candiate Object     
  14.   Name = name;  
  15.   EducationProfileId = id;  
  16.   educationProfilesList = new Lazy < List < EducationProfile < string >>> (() => {  
  17.    return GetEducationProfileList(id);  
  18.   });  
  19.   //Initialization done        
  20.  }  
  21.   
  22.  private List < EducationProfile < string >> GetEducationProfileList(T id) {  
  23.   //Loading EducationProiles        
  24.   List < EducationProfile < string >> EducationList = new List < EducationProfile < string >> ();  
  25.   
  26.   Parallel.For(100, 110, (int i) => {  
  27.    EducationProfile < string > educationprofile = new EducationProfile < string > ();  
  28.    educationprofile.Id = i.ToString();  
  29.    educationprofile.Class = "MCA";  
  30.    educationprofile.PassingYear = "2012";  
  31.    EducationList.Add(educationprofile);  
  32.   });  
  33.   return EducationList;  
  34.  }  
  35. } 

Initialize the Method

 
The method definition for the CallLazyLoading() is given below
  1. public static void CallLazyLoading()  
  2. {  
  3.   
  4.             Candiate<string> candiate = new Candiate<string>("Nirmal dayal""1");  
  5.   
  6.             Console.WriteLine("Name:{0}", candiate.Name);  
  7.             Console.WriteLine("\n");  
  8.             foreach (EducationProfile<string> eduProfile in candiate.educationProfilesList.Value)  
  9.             {  
  10.                 Console.WriteLine("Id:{0}", eduProfile.Id);  
  11.                 Console.WriteLine("Degree:{0}", eduProfile.Class);  
  12.                 Console.WriteLine("Passing Year:{0}", eduProfile.PassingYear);  
  13.                 Console.WriteLine("\n");  
  14.             }  
Click on F5 and execute the project and follow the console window. The output will be
 
image3 
 
I hope this article is helpful for the design of lazy loading with generic class when you want to begin working in the ASP.NET 5 console applications. Thanks for reading this article.
 
Happy Coding!


Similar Articles