Home
Gallery
GuestBook
SourceCode
Contact

Lazy Loading of Objects

Posted in .NET
This post has been read 581 times

Lazy loading of objects essentially means that the data is loaded only when requested and not when the object instance is created.

For Eg. suppose we have a Blog class which has a blogText property. Now this property can be encapsulated like so.


    1 public string BlogText

    2 {

    3     get

    4     {

    5         if (blogText == null)

    6             blogText = SiteProvider.Blogs.GetBlogBody(Id);

    7         return blogText;

    8     }

    9     set { blogText = value; }

   10 }

Here the BlogText property implements the lazy load pattern which means that the blogText field is retrieved only when it's being accessed by another class. Hence if this property is not accessed from anywhere then the data will not be read from the database. Once the data is retrieved it will be held in memory for subsequent requests, hence i am checking if the value is null, if not then only hit the database and get the value.




Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll