Skip to main content

How to perform lazy loading with generic method.

Introduction


Let’s develop 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.



Now we will select the ASP.NET 5 Console Application because we will create the console application and click on the OK button.



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. }

Initialize 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



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!

Comments

Popular posts from this blog

How to find the count to convert a pyramid into a square using C#.net

Introduction   Let’s develop a simple C# console application to find the count of convert a pyramid into a square. 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. Now we will select the ASP.NET 5 Console Application to create the console application, click on the OK button. Include the following references in the application: using  System;   using  System.Collections.Generic;   using  System.Linq;  In the main method, I have created 2 methods 1.PrintPyramid – To print the triangle in given input. 2.ConvertPyramidToSquare = Convert triangle into square. Please refer the code snippet below: static   void  Main( string [] args)   {   try    {   PrintPyramid();   ConvertPyram...

Open Closed Principle

  Open Closed Principle We will be discussing the Open-Closed Principle also known as OCP, as one of the SOLID principles of object-oriented programming and how to implement it when designing our software. This principle said that should be open for extension but closed for modification. We should write a code that does not require modification every time a customer changes its request. Example Let us define a class that contains salary calculations. Let us imagine that we have a task where we need to calculate the total cost of all the developer salaries in a single company. To get started, we are going to create the model class first, public class EmployeeReport { public int Id { get ; set ; } public string Name { get ; set ; } public string Level { get ; set ; } public int WorkingHours { get ; set ; } public double HourlyRate { get ; ...

Liskov Substitution Principle

Liskov Substitution Principle We will be discussing the Liskov Substitution Principle also known as LSP, as one of the SOLID principles of object-oriented programming and how to implement it when designing our software. This principle said that Subtypes must be substitutable for their base types. In other words, if we substitute a superclass object reference with an object of any of its subclasses, the program should not break. Example Here we design three classes for employees and one abstract class and Implement abstract class on these classes, PermamentEmployee.cs ContractEmployee.cs TemporaryEmployee.cs class LSP { public LSP ( ) { Run ( ) ; } public void Run ( ) { Console . ReadLine ( ) ; } public abstract class Employee { protected string FullName { get ; set ; } protected abstract string CalculateBonus ( ) ; } public class PermamentEmployee : Employee ...