THE SQL Server Blog Spot on the Web

Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | |
in Search

Jamie Thomson

This is the blog of Jamie Thomson, a freelance data mangler in London

ObjectStorageHelper<T> now available for Windows 8 RTM

In October 2011 I wrote a blog post entitled ObjectStorageHelper<T> – A WinRT utility for Windows 8 where I introduced a little utility class called ObjectStorageHelper<T> that I had been working on while noodling around on the Developer Preview of Windows 8. ObjectStorageHelper<T> makes it easy for anyone building apps for Windows 8 to save data to files. How easy? As easy as this:

var myPoco = new Poco() { IntProp = 1, StringProp = "one" };
var objectStorageHelper = new ObjectStorageHelper<Poco>(StorageType.Local);
await objectStorageHelper.SaveAsync(myPoco);

Compare that to the plumbing code that you would have to write otherwise:

var Obj = new Poco() { IntProp = 1, StringProp = "one" };
var filename = “SomeFileName”; 
StorageFile file = null;
StorageFolder folder = GetFolder(storageType);
file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite);
using (Stream outStream = Task.Run(() => writeStream.AsStreamForWrite()).Result)
{
    serializer.Serialize(outStream, Obj);
    await outStream.FlushAsync();
}

and you can see how ObjectStorageHelper<T> can help save a Windows 8 developer quite a few headaches. ObjectStorageHelper<T> simply requires you to pass it an object to be saved, tell it where to save it (Roaming, Local or Temporary), and you’re done. You don’t even need to give it a filename.

Retrieving an object from storage is equally as simple:

var objectStorageHelper = new ObjectStorageHelper<Poco>(StorageType.Local);
var myPoco = await objectStorageHelper.LoadAsync();

Please check the homepage for the project at http://winrtstoragehelper.codeplex.com/ for (much) more info.


A number of people have used and tested ObjectStorageHelper<T> since those early days and one of those folks in particular, David Burela, was good enough to submit a couple of bugs:

As a result of David’s bug reports and some more extensive testing on my side I have overhauled the initial code that I wrote last October and am confident that it is now much more robust and ready for primetime (check the commit history if you’re interested). The source code (which, again, you can find on Codeplex at http://winrtstoragehelper.codeplex.com/) includes a suite of unit tests to test all of the basic use cases (if you can think of any more please let me know).

If you use this in any of your Windows 8 projects then please let me know. I love getting feedback and I’d also love to know if this is actually being used anywhere.

@Jamiet

Published Wednesday, August 29, 2012 12:20 AM by jamiet

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Ian Yates said:

Looks like a great idea.  I'm downloading the source code soon although time at the moment won't let me start anything with actual Win 8 development for a few weeks unfortunately :(   My issue is trying to bring a legacy codebase up to look modern but still supporting a lot of customers who use terminal services with Server 2008/R2 (a few still on 2003 but we've been telling them for a while that that's not sustainable).  I am thinking a simple Metro companion app to a .NET 4.5 desktop client is the way to go for us, but we'll see.

Anyway, your ObjectStorageHelper<T> looks like a very elegant solution to an obvious problem - thanks! :)

August 29, 2012 7:15 AM
 

jamiet said:

Thanks Ian. If you do find time to use it I'd love to know what you think.

August 29, 2012 7:22 AM
 

Jeff said:

I can't seem to get it to save multiple records into 1 file.  What is the best way to do this?

September 14, 2013 11:56 AM

Leave a Comment

(required) 
(required) 
Submit

This Blog

Syndication

Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement