Recent Entries 2
- pattern minor 112d agoIsoStorageManagerA manager for a speedy async saving objects to isolated storage, using serialization from Newtonsoft.Json. A project to play with is here. ``` 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
- pattern minor 112d agoSimplest WP7 loggerJust ended up with this approach for wp7 (no tag yet), in case someone would find it useful. Also, improvement considerations are welcome. It works with - `SimpleLogger.WriteLine("JustLine");` - `SimpleLogger.WriteLine(ObjectToBeCastedToString);` - `SimpleLogger.WriteLine("Price is {0} {1}", price, currency);` ``` public class SimpleLogger { private static DateTime lastLog; [Conditional("DEBUG")] public static void WriteLine(object value) { WriteLine((value == null) ? "(null)" : value.ToString()); } [Conditional("DEBUG")] public static void WriteLine(string format) { WriteLine("{0}", format); } [Conditional("DEBUG")] public static void WriteLine(string format, params object[] values) { var formatted = String.Format(null, format, values); Debug.WriteLine("{0:hh:mm:ss.fff} [{1:hh:mm:ss.fff}] {2}", DateTime.UtcNow, DateTime.UtcNow - lastLog, formatted); lastLog = DateTime.UtcNow; } } ```