Lazy Initialization or Early initialization?

Topics: ASP.NET 2.0
Jul 4, 2007 at 5:18 AM
Load the Content and Comment when we want to use then.

I think load all post(include all content/comment/media data fist in memery seems like a lot of a waste


Lazy Initialization is better then load the large object in memery first(Early initialization)

Post class in Business object layer

private string _Content;
/// <summary>
/// Gets or sets the Content or the post.
/// </summary>
public string Content
{
get
{
if ( _Content == null )
{
_Content = LoadPostContent( this.Id );
}
return _Content;
}
set
{
if ( _Content != value )
MarkDirty( "Content" );
_Content = value;
}
}
private string LoadPostContent(Guid id)
{
string content = null;

string key = string.Format("Be:Content:{0}",id);

// if there is no content cached
object obj = HttpContext.Current.Cache.Get(key);
if(obj == null)
{
// load the post's content from provider here
content = BlogService.LoadPostContent( id );

HttpContext.Current.Cache.Insert(key, content, null, DateTime.Now.AddMinutes(2), TimeSpan.Zero);

// if use xml store the data
// we can use a CacheDependency like new CacheDependency(_Folder + "posts\\" + id.ToString() + ".xml") in the Cache.Insert Method
return content;
}
return (strong)obj;

// about cache
//http://www.aspxboy.com/code/default.aspx/S7s2zPJbeI9PJA/OKHpAVwVaUEhf02EgEOgeB3Yd5Z7fLL3ZNgdv7XIqertUOYm9swwXqZ4525aiPgV7zXEob8bofuU3o3aaupA3VCA9GE2jSquFT4SLb7nLDuMmKMagj0QiqYE6ydg=.Aspx

}

private static object _SyncRoot = new object();
private static List<Post> _Posts;
/// <summary>
/// A sorted collection of all posts in the blog.
/// Sorted by date.
/// </summary>
public static List<Post> Posts
{
get
{
lock (_SyncRoot)
{
if (_Posts == null)
{
//in provider the 'FillPosts' method' dose not return the 'real' content' per post;

_Posts = BlogService.FillPosts( );
}
return _Posts;
}
}
}

in XmlBlogProvider

/// <summary>
/// Retrieves a post based on the specified Id.
/// </summary>
public override Post SelectPost(Guid id)
{
string fileName = _Folder + "posts\\" + id.ToString() + ".xml";
Post post = new Post();
XmlDocument doc = new XmlDocument();
doc.Load(fileName);

post.Title = doc.SelectSingleNode("post/title").InnerText;
post.Description = doc.SelectSingleNode("post/description").InnerText;

post.Content = null; // dose not return the 'real' content'

post.DateCreated = DateTime.Parse(doc.SelectSingleNode("post/pubDate").InnerText);
post.DateModified = DateTime.Parse(doc.SelectSingleNode("post/lastModified").InnerText);

// setting other filed

return post;
}
Post class in Business object layer

private string _Content;
/// <summary>
/// Gets or sets the Content or the post.
/// </summary>
public string Content
{
get
{
if ( _Content == null )
{
_Content = LoadPostContent( this.Id );
}
return _Content;
}
set
{
if ( _Content != value )
MarkDirty( "Content" );
_Content = value;
}
}
private string LoadPostContent(Guid id)
{
string key = string.Format("Be:Content:{0}",id);
string content = null;
// if there is no content cached by id
object obj = HttpContext.Current.Cache.Get(key);
if(obj == null)
{
// load the post's content from provider here
content = BlogService.LoadPostContent( id );

HttpContext.Current.Cache.Insert(key, content, null, DateTime.Now.AddMinutes(2), TimeSpan.Zero);

// if use xml store the data
// we can use a CacheDependency like new CacheDependency(_Folder + "posts\\" + id.ToString() + ".xml") in the Cache.Insert Method
}

// about cache
//http://www.aspxboy.com/code/default.aspx/S7s2zPJbeI9PJA/OKHpAVwVaUEhf02EgEOgeB3Yd5Z7fLL3ZNgdv7XIqertUOYm9swwXqZ4525aiPgV7zXEob8bofuU3o3aaupA3VCA9GE2jSquFT4SLb7nLDuMmKMagj0QiqYE6ydg=.Aspx

}

private static object _SyncRoot = new object();
private static List<Post> _Posts;
/// <summary>
/// A sorted collection of all posts in the blog.
/// Sorted by date.
/// </summary>
public static List<Post> Posts
{
get
{
lock (_SyncRoot)
{
if (_Posts == null)
{
//in provider the 'FillPosts' method' dose not return the 'real' content' per post;

_Posts = BlogService.FillPosts( );
}
return _Posts;
}
}
}

in XmlBlogProvider

/// <summary>
/// Retrieves a post based on the specified Id.
/// </summary>
public override Post SelectPost(Guid id)
{
string fileName = _Folder + "posts\\" + id.ToString() + ".xml";
Post post = new Post();
XmlDocument doc = new XmlDocument();
doc.Load(fileName);

post.Title = doc.SelectSingleNode("post/title").InnerText;
post.Description = doc.SelectSingleNode("post/description").InnerText;

post.Content = null; // dose not return the 'real' content'

post.DateCreated = DateTime.Parse(doc.SelectSingleNode("post/pubDate").InnerText);
post.DateModified = DateTime.Parse(doc.SelectSingleNode("post/lastModified").InnerText);

// setting other fileds

return post;
}

any response was grateful, thanks !