Pages

Saturday, September 10, 2011

Lazy Loading in C# 4.0(Object On Demand)

Object on Demand is also called as Lazy loading pattern ,Lazy loading  delay the initialization of object. This is a new feature of C# 4.0 its can be used when we are working with large objects when its not in use. This article will explain you about "Lazy" class.
Suppose 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 which the respective Candidate.You need to load EducationProfiles associated with that Candidate. If  You are loading a Education Profile it with the respective candidate . you need to initialize a Candidate object and that is suppose 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 example of Candidate and  EducationProfile relationship by Lazy loading:
First we need to create ay 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> educationProileList;

    public Candiate(string name, int id)
    {
        //Initializing Candiate Object
        Name = name;
        EducationProfileId = id;
        educationProileList = new Lazy>(() => { 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;
    }
}

In the constructor of Candidate class, properties are initializing and declaring educationProileList object, which is generic List of educationProile and filled by GetEducationProfileList method. This method will only call when educationProileList object will be use. Below is main method which shows behavior of lazy loading:
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();
}

1 comment:

  1. I am very week in programming.thanks for sharing this code.Now I can make different programms from this code.

    ReplyDelete