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> – A WinRT utility for Windows 8

[Disclaimer: This blog post was written in the Developer Preview timeframe and the code has since been heavily updated. Please visit my ObjectStorageHelper feed to see the most up to date information.] 

A heads up – this blog post has nothing to do with SQL Server. You have been warned.

Introduction

Last month Microsoft announced the next version of Windows and along with it a new programming model called Windows Runtime (WinRT). Given my long-time interest in the Live Framework API which has re-emerged (sort of) in WinRT I decided to have poke around in what it had to offer. I discovered that any application written for WinRT will have access to three sandboxed folders:

  • LocalFolder
  • TemporaryFolder
  • RoamingFolder

LocalFolder and TemporaryFolder are fairly self-explanatory. RoamingFolder is where the remnants of the Live Framework API rears its head – the contents of that folder gets synced to any device on which the app is installed – thus providing the ability to sync application data between those different devices; I’m not going to go into the scenarios that this feature makes possible but I will say that I think this is one of the most exciting features about Windows 8.

As I poked and prodded at the WinRT classes provided to enable interaction with these three folders I concluded that it wasn’t exactly straightforward and figured I’d have a go at writing something a bit friendlier and hence ObjectStorageHelper<T> was born. It is, in short, a simple class that provides three basic methods:

  • void DeleteASync()
  • void SaveASync(T Object)
  • Task<T> LoadASync()

Saving an object

They simply allow you to manage storage of nearly any object (basically anything that can be serialized as XML). Here’s the code to save an object, its only two lines of code:

[TestMethod]
public void SaveObject()
{
  //Instantiate an object that we want to save
  var myPoco = new Poco() { IntProp = 1, StringProp = "one" };
  //new up ObjectStorageHelper specifying that we want to interact with the Local storage folder
  var objectStorageHelper = new ObjectStorageHelper<Poco>(StorageType.Local);
  //Save the object (via XML Serialization) to the specified folder, asynchronously
  objectStorageHelper.SaveASync(myPoco);
}

The name of the file that gets stored is determined by the name of the type T specified in ObjectStorageHelper<T> and this in turn means that only one object of type T can be stored in each of the three folders; this is by design.
If you have a need to store multiple objects of T then you could use a List instead like this: ObjectStorageHelper<List<T>> or simply adjust the code herein so that it allows you to specify a filename.

Retrieving an object

Retrieving that object thereafter is equally as easy, just two lines of code

[TestMethod]
public async void LoadObject()
{
  //new up ObjectStorageHelper specifying that we want to interact with the Local storage folder
  var objectStorageHelper = new ObjectStorageHelper<Poco>(StorageType.Local);
  //Get the object from the storage folder
  Poco myPoco = await objectStorageHelper.LoadASync();
}

Get the goods

If you want to have a play then the code or compiled assembly is available under the MS-PL license on Codeplex at http://winrtstoragehelper.codeplex.com/. Feedback is welcome!

@jamiet

Published Friday, October 21, 2011 9:51 PM 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

 

Richard said:

OK, nit-picking time:

In .NET, an asynchronous method uses the suffix "Async".

Your methods use the suffix "ASync".

Does that mean your methods are going to load a "sync", or that they're going to load "A" synchronously? :)

November 3, 2011 4:09 PM
 

jamiet said:

Richard,

Good point - I'm a stickler for using the right convnentions so I'll definitely sort that out. Thanks for letting me know.

Regards

JT

November 4, 2011 4:57 AM
 

hp said:

it will be nice to split the physical files for large collections storage. Say my list has 1000 elements and I want to store them all, it will be nice to specify that store X objects in one file aka have 1000/x files on the storage. I would expect the read to be like read(x objects) as well. splitting files will mean disk io performance is better for paged reading.  

April 7, 2012 3:57 PM
 

SSIS Junkie said:

In October 2011 I wrote a blog post entitled ObjectStorageHelper&lt;T&gt; – A WinRT utility for Windows

August 28, 2012 6:20 PM
 

Apple said:

THIS PIECE OF SHIT DOESNT WORK. IT IS GARBAGE !

August 29, 2012 3:36 PM
 

jamiet said:

Thanks for the great feedback, Apple.

Are you using the latest version that I announced yesterday: http://sqlblog.com/blogs/jamie_thomson/archive/2012/08/29/objectstoragehelper-t-now-available-for-windows-8-rtm.aspx ?

JT

August 29, 2012 5:54 PM
 

Kent Chenery said:

Hey Apple,

Jamie isn't here to do your job for you.  He's trying to help and educate us.  He does it in his own time and he's not paid for it (AFAIK).  If he's done something wrong, feel free to provide some constructive criticism but simply spouting that kind of reply helps nobody.  Least of all yourself.  You could have simply come back with a reply more along the lines of "Hey Jamie, I'm struggling to get this to work.  Here's what's happening.....any ideas?".  You'd probably be quite surprised how helpful he is!

So next time you're about to "crap" all over someone's blog; think twice.  You'll get a lot more support from the community with a few platitudes thrown around here and there.

Kent

August 30, 2012 4:30 AM

Leave a Comment

(required) 
(required) 
Submit

This Blog

Syndication

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