Created
October 3, 2018 14:36
-
-
Save jraps20/4bdb4e75591dd17dfe17bd738c3cdd61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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