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.
- public class EducationProfile
- {
- public int Id { get; set; }
- public string Digree { get; set; }
- public DateTime PassingYear { get; set; }
- }
- //Hope this will give you a little reference of lazy loading
- public class Candiate
- {
- public string Name { get; set; }
- public int EducationProfileId { get; set; }
- public List GetAllEducationProfile()
- {
- return educationProileList.value;
- }
- Lazy<list<educationproile>> educationProileList;
- public Candiate(string name, int id)
- {
- //Initializing Candiate Object
- Name = name;
- EducationProfileId = id;
- educationProileList = new Lazy<list<educationproile>>(() => { return GetEducationProfileList(id); });
- //Initialization done
- }
- private List GetEducationProfileList(int id)
- {
- //Loading EducationProiles
- List list = new List();
- Parallel.For(100, 110, (int i) =>
- {
- EducationProile educationprofile = new EducationProile();
- educationprofile.Id = i;
- list.Add(educationprofile);
- });
- return list;
- }
- }
- </list<educationproile></list<educationproile>
- public static void Main(string[] args)
- {
- Candidate candidate = new Candidate("AnyName", 1);
- foreach(EducationProile eduProfile in candiate.GetAllEducationProfile())
- // it will actually load accounts, ie. lazy loading
- Console.WriteLine("Id:{0}",eduProfile.Id);
- Console.Read();
- }

vibhor dhawanPosted Oct 8, 2021, 5:42 AM
A lot of typos and others issues with this code.
Trần KhoaPosted May 6, 2020, 9:47 PM
You make alot of mistakes in your code.
flamecrow flamecrowPosted Dec 13, 2019, 2:22 PM
Holy shit typos.
Mahesh AnakaliPosted Jul 22, 2019, 8:59 AM
Nice Article, But lot of errors when I copy pasted the code into editor.
Santhakumar MunuswamyPosted Oct 18, 2015, 3:26 AM
Good one
Shakti SaxenaPosted Oct 15, 2015, 7:54 AM
Good one
Vipul MalhotraPosted Oct 14, 2015, 5:42 AM
nice article. Thanks for sharing
Humayun Kabir MamunPosted Oct 14, 2015, 12:18 AM
Nice...
Gopi ChandPosted Oct 13, 2015, 10:57 AM
Nice work
Sujeet SumanPosted Oct 13, 2015, 10:37 AM
Good Article.............
RakeshPosted Oct 13, 2015, 10:18 AM
Good share
Mukesh KumarPosted Oct 13, 2015, 8:32 AM
Great.. thanks for sharing
Nilesh JadavPosted Oct 13, 2015, 8:26 AM
Good Share sir
Harshad PansuriyaPosted Oct 13, 2015, 8:22 AM
Nice one