THE SQL Server Blog Spot on the Web

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

SSIS Junkie

This is the blog of Jamie Thomson, a freelance SQL Server developer in London

ObjectStorageHelper<T> – A WinRT utility for Windows 8

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
Filed under: ,

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

 

The Morning Brew - Chris Alcock » The Morning Brew #965 said:

October 24, 2011 3:43 AM
 

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

Leave a Comment

(required) 
(optional)
(required) 
Submit

This Blog

Syndication

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