Created
January 15, 2013 22:21
-
-
Save JoeRobich/4542694 to your computer and use it in GitHub Desktop.
Simple KeyValueStore that I wrote and was unable to use.
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
public interface IKeyValueStore | |
{ | |
object this[string key] { get; set; } | |
} | |
public static class KeyValueStoreExtensions | |
{ | |
public static DateTime GetDateTime(this IKeyValueStore store, string key) | |
{ | |
var val = store[key]; | |
if (val == null) | |
return DateTime.MinValue; | |
return DateTime.Parse(val.ToString()); | |
} | |
} | |
public class MemoryStore : Dictionary<string, object>, IKeyValueStore | |
{ | |
} | |
public class RegistryStore : IKeyValueStore | |
{ | |
private MemoryStore _memoryStore = new MemoryStore(); | |
private string _keyName = string.Empty; | |
public RegistryStore(string keyName) | |
{ | |
if (string.IsNullOrEmpty(keyName)) | |
throw new ArgumentNullException("keyName"); | |
_keyName = keyName; | |
} | |
public object this[string key] | |
{ | |
get | |
{ | |
// Use a memory store as a cache to avoid hitting the registry on each request. | |
if (!_memoryStore.ContainsKey(key)) | |
_memoryStore[key] = Registry.GetValue(_keyName, key, null); | |
return _memoryStore[key]; | |
} | |
set | |
{ | |
_memoryStore[key] = value; | |
Registry.SetValue(_keyName, key, value); | |
} | |
} | |
} | |
public class Storage | |
{ | |
private IKeyValueStore _store; | |
public Storage() | |
: this(new MemoryStore()) | |
{ | |
} | |
public Storage(IKeyValueStore store) | |
{ | |
_store = store; | |
} | |
public DateTime LastModifiedDate | |
{ | |
get | |
{ | |
return _store.GetDateTime("LastModifiedDate"); | |
} | |
set | |
{ | |
_store["LastModifiedDate"] = value; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment