Friday, November 28, 2008

Lazy loading class




///
/// Lazy loading class. When the value is required object will ask for item provider and cache result.
///

/// Item type.
/// Key type.
internal class LazyLoadingBase
{
public delegate TItem GetItemWithNameDelegate(TKey name);

GetItemWithNameDelegate getItemWithName;

TKey key;
TItem item;
bool itemSet;

public LazyLoadingBase(GetItemWithNameDelegate GetItemWithName)
{
itemSet = false;
item = default(TItem);
getItemWithName = GetItemWithName;
}

public object Value
{
get
{
if (!itemSet)
{
item = getItemWithName(key);
itemSet = true;
}
return item;
}

set
{
if (value is TItem)
{
item = (TItem)value;
itemSet = true;
}
else if (value is TKey)
{
key = (TKey)value;
}
else
{
throw new InvalidOperationException();
}
}
}

///
/// Cast operator.
///

public static implicit operator TItem(LazyLoadingBase lazyLoadingBaseObject)
{
return (TItem)lazyLoadingBaseObject.Value;
}
}


1 comment:

Unknown said...

Casino Games at Jordan's Casino
Casino how can i get air jordan 18 retro Games at Jordan's Casino is the ultimate destination for entertainment and entertainment, from classic slots air jordan 18 retro toro mens sneakers sports and table games find air jordan 18 stockx to live ‎Casino air jordan 18 retro toro mens sneakers to us Games · ‎Contact · ‎Casino show to get air jordan 18 retro yellow Locations