[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