Skip to content

Instantly share code, notes, and snippets.

@damianh
Created February 14, 2012 11:22
Show Gist options
  • Save damianh/1825986 to your computer and use it in GitHub Desktop.
Save damianh/1825986 to your computer and use it in GitHub Desktop.
Storage (Repository Discussion)
//Guard clauses / contracts omitted for berivity
//Thread safety not considered.
public interface IKeyValueStore
{
void Add<T>(string key, T item);
void AddOrUpdate<T>(string key, T item);
bool Remove(string key);
bool TryGetValue<T>(string key, out T item);
}
public class IsolatedStorageSettingsKeyValueStore : IKeyValueStore
{
private readonly IsolatedStorageSettings _isolatedStorageSettings;
public IsolatedStorageSettingsKeyValueStore(IsolatedStorageSettings isolatedStorageSettings)
{
_isolatedStorageSettings = isolatedStorageSettings;
}
public void Add<T>(string key, T item)
{
_isolatedStorageSettings.Add(key, item);
}
public void AddOrUpdate<T>(string key, T item)
{
T storedItem;
if(TryGetValue(key, out storedItem))
{
if (!item.Equals(storedItem))
{
_isolatedStorageSettings[key] = item;
}
}
_isolatedStorageSettings.Add(key, item);
}
public bool Remove(string key)
{
return _isolatedStorageSettings.Remove(key);
}
public bool TryGetValue<T>(string key, out T item)
{
return _isolatedStorageSettings.TryGetValue(key, out item);
}
}
public class TypicalNinjectModule : NinjectModule
{
public override void Load()
{
Bind<IKeyValueStore>().To<IsolatedStorageSettingsKeyValueStore>();
Bind<IsolatedStorageSettings>().ToMethod(ctx => IsolatedStorageSettings.ApplicationSettings);
}
}
@damianh
Copy link
Author

damianh commented Feb 14, 2012

The problem with these sort of abstractions over arbitrary 'storage' is that they invariably become leaky.

@damianh
Copy link
Author

damianh commented Feb 14, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment