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 { get; set; }
  4. public T Class { get; set; }
  5. public T PassingYear { get; set; }
  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. public Candiate(T name, T id) {
  12. //Initializing Candiate Object
  13. Name = name;
  14. EducationProfileId = id;
  15. educationProfilesList = new Lazy < List < EducationProfile < string >>> (() => {
  16. return GetEducationProfileList(id);
  17. });
  18. //Initialization done
  19. }
  20. private List < EducationProfile < string >> GetEducationProfileList(T id) {
  21. //Loading EducationProiles
  22. List < EducationProfile < string >> EducationList = new List < EducationProfile < string >> ();
  23. Parallel.For(100, 110, (int i) => {
  24. EducationProfile < string > educationprofile = new EducationProfile < string > ();
  25. educationprofile.Id = i.ToString();
  26. educationprofile.Class = "MCA";
  27. educationprofile.PassingYear = "2012";
  28. EducationList.Add(educationprofile);
  29. });
  30. return EducationList;
  31. }
  32. }

Initialize the Method

The method definition for the CallLazyLoading() is given below
  1. public static void CallLazyLoading()
  2. {
  3. Candiate<string> candiate = new Candiate<string>("Nirmal dayal", "1");
  4. Console.WriteLine("Name:{0}", candiate.Name);
  5. Console.WriteLine("\n");
  6. foreach (EducationProfile<string> eduProfile in candiate.educationProfilesList.Value)
  7. {
  8. Console.WriteLine("Id:{0}", eduProfile.Id);
  9. Console.WriteLine("Degree:{0}", eduProfile.Class);
  10. Console.WriteLine("Passing Year:{0}", eduProfile.PassingYear);
  11. Console.WriteLine("\n");
  12. }
  13. }
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!