Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
namespace MyProject.Extensions
{
public static class ItemsContextExtensions
{
public static Item Get(this ItemsContext source, string identifier)
{
if (string.IsNullOrWhiteSpace(identifier))
throw new Exception("Must set identifier for getting global property");
var dictionary = source as IDictionary;
if (!dictionary.Contains(identifier))
return null;
var value = dictionary[identifier];
if (value.GetType() == typeof(Item))
{
return value as Item;
}
if (!(value.GetType() == typeof(Func<Item>)))
return null;
var lazyValue = value as Func<Item>;
var resultValue = lazyValue() as Item;
dictionary[identifier] = resultValue;
return resultValue;
}
public static void SetLazy(this ItemsContext source, string identifier, Func<Item> lazyValue)
{
if (string.IsNullOrWhiteSpace(identifier))
throw new Exception("Must set identifier for setting global property");
var dictionary = source as IDictionary;
dictionary[identifier] = lazyValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment