patterncsharpMinor
IsoStorageManager
Viewed 0 times
isostoragemanagerstackoverflowprogramming
Problem
A manager for a speedy async saving objects to isolated storage, using serialization from Newtonsoft.Json. A project to play with is here.
Where extensions are
```
private static readonly JsonSerializer JsonSerializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore };
public static async Task WriteJsonAsyncTask(this JsonTextWriter writer, T content)
{
writer.Formatting = Formatting.Indented;
return await TaskEx.Run(() =>
{
try { JsonSerializer.Serialize(writer, content); }
catch (Exception) { return false; }
return true;
});
}
public static async Task ReadJsonAsyncTask(this JsonTextReader reader)
{
return await TaskEx.Run(() => JsonSerializer.Deseria
public static async Task ReadJsonEx(String filepath)
{
if (String.IsNullOrEmpty(filepath))
return default(T);
return await await Task.Factory.StartNew(async () =>
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var stream = new IsolatedStorageFileStream(filepath, FileMode.Open, store))
using (var sr = new StreamReader(stream))
using (var jr = new JsonTextReader(sr))
return await jr.ReadJsonAsyncTask();
});
}
public static async Task WriteJsonEx(String filepath, T content)
{
if (String.IsNullOrEmpty(filepath))
return false;
return await await Task.Factory.StartNew(async () =>
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var stream = new IsolatedStorageFileStream(filepath, FileMode.Create, store))
using (var sw = new StreamWriter(stream))
using (var jw = new JsonTextWriter(sw))
await jw.WriteJsonAsyncTask(content);
return true;
});
}Where extensions are
```
private static readonly JsonSerializer JsonSerializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore };
public static async Task WriteJsonAsyncTask(this JsonTextWriter writer, T content)
{
writer.Formatting = Formatting.Indented;
return await TaskEx.Run(() =>
{
try { JsonSerializer.Serialize(writer, content); }
catch (Exception) { return false; }
return true;
});
}
public static async Task ReadJsonAsyncTask(this JsonTextReader reader)
{
return await TaskEx.Run(() => JsonSerializer.Deseria
Solution
This is very neat. Just a few code formatting nitpicks:
Call me over-zealous, I think it would be best to specify the scopes with curly braces - I like the stacked
And here too:
I find code reads better going down than going across:
if (String.IsNullOrEmpty(filepath))
return default(T);
return await await Task.Factory.StartNew(async () =>
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var stream = new IsolatedStorageFileStream(filepath, FileMode.Open, store))
using (var sr = new StreamReader(stream))
using (var jr = new JsonTextReader(sr))
return await jr.ReadJsonAsyncTask();
});Call me over-zealous, I think it would be best to specify the scopes with curly braces - I like the stacked
using blocks though, so like this:if (String.IsNullOrEmpty(filepath))
{
return default(T);
}
return await await Task.Factory.StartNew(async () =>
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var stream = new IsolatedStorageFileStream(filepath, FileMode.Open, store))
using (var sr = new StreamReader(stream))
using (var jr = new JsonTextReader(sr))
{
return await jr.ReadJsonAsyncTask();
}
});And here too:
public static async Task WriteJsonAsyncTask(this JsonTextWriter writer, T content)
{
writer.Formatting = Formatting.Indented;
return await TaskEx.Run(() =>
{
try { JsonSerializer.Serialize(writer, content); }
catch (Exception) { return false; }
return true;
});
}I find code reads better going down than going across:
public static async Task WriteJsonAsyncTask(this JsonTextWriter writer, T content)
{
writer.Formatting = Formatting.Indented;
return await TaskEx.Run(() =>
{
try
{
JsonSerializer.Serialize(writer, content);
return true;
}
catch (Exception)
{
return false;
}
});
}Code Snippets
if (String.IsNullOrEmpty(filepath))
return default(T);
return await await Task.Factory.StartNew(async () =>
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var stream = new IsolatedStorageFileStream(filepath, FileMode.Open, store))
using (var sr = new StreamReader(stream))
using (var jr = new JsonTextReader(sr))
return await jr.ReadJsonAsyncTask<T>();
});if (String.IsNullOrEmpty(filepath))
{
return default(T);
}
return await await Task.Factory.StartNew(async () =>
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var stream = new IsolatedStorageFileStream(filepath, FileMode.Open, store))
using (var sr = new StreamReader(stream))
using (var jr = new JsonTextReader(sr))
{
return await jr.ReadJsonAsyncTask<T>();
}
});public static async Task<bool> WriteJsonAsyncTask<T>(this JsonTextWriter writer, T content)
{
writer.Formatting = Formatting.Indented;
return await TaskEx.Run(() =>
{
try { JsonSerializer.Serialize(writer, content); }
catch (Exception) { return false; }
return true;
});
}public static async Task<bool> WriteJsonAsyncTask<T>(this JsonTextWriter writer, T content)
{
writer.Formatting = Formatting.Indented;
return await TaskEx.Run(() =>
{
try
{
JsonSerializer.Serialize(writer, content);
return true;
}
catch (Exception)
{
return false;
}
});
}Context
StackExchange Code Review Q#47897, answer score: 3
Revisions (0)
No revisions yet.