Skip to content

Instantly share code, notes, and snippets.

@jraps20
Created October 3, 2018 14:36
Show Gist options
  • Save jraps20/4bdb4e75591dd17dfe17bd738c3cdd61 to your computer and use it in GitHub Desktop.
Save jraps20/4bdb4e75591dd17dfe17bd738c3cdd61 to your computer and use it in GitHub Desktop.
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