StorageHelper NuGet package

UPDATE June 22, 2014: Version 1.1.4 of StorageHelper is now available through NuGet, adding settings APIs for Windows Store (Windows 8.1, Windows Phone 8.1 and Universal) apps. Please note that there was a change in namespaces to clean things up and Windows Phone 7.1 and Windows 8.0 support was dropped (you can still install 1.1.3 for those projects. On the upside: I’ve open-sourced the package through GitHub, so you can contribute if needed!


If you’ve taken a look at any of my starting templates (Windows Phone or Windows) or at my Windows Phone RSS Reader app template, you’ve probably noticed the StorageHelper class in there. It’s a convenient way of abstracting some of the logic of loading, saving and removing data from either the application’s state or persistent storage. I’ve now extracted that class and made it available in a NuGet package, so it’s easier to re-use in projects, without having to copy a class file over.

Installing the package

You can either use the Package Manager Console or the NuGet GUI in Visual Studio to install the package. In the Package Manager Console, simply use the following command:

Install-Package StorageHelper

The package will automatically add the proper library reference according to the type of project you’re adding it to (Windows Phone 7, Windows Phone 8 or Windows 8). If you’re using the NuGet GUI in Visual Studio, simply search for StorageHelper and install it that way.

Usage

Windows Phone 7

As Windows Phone 7 does not have native support for the async/await pattern, the functionality in StorageHelper is also synchronous in nature. The following methods are available on Windows Phone 7:

Storage.SaveState("MyKey", myObj);
Storage.LoadState<TYPE>("MyKey");
Storage.DeleteState("MyKey");

Storage.Save("MyKey", myObj);
Storage.Load<TYPE>("MyKey");
Storage.Delete("MyKey");

The first three methods will save, load or delete an object from the PhoneApplicationService.Current.State dictionary, meaning it will only be there as long as your app is loaded into memory. A good use of these methods is when implementing the behavior that will be used when your app is tombstoned, for example to store form data temporarily to be restored when the user navigates back to the app.

The last three methods will save, load or delete an object from the IsolatedStorageSettings.ApplicationSettings dictionary, which will be persisted on the device. Use these methods when saving settings or data that will need to be accessible between sessions in the app.

Windows Phone 8

With the release of Windows Phone 8, we got SDK support for the async/await pattern. The methods available on Windows Phone 8 are largely the same as on Windows Phone 7, with the exception of async/await support for the persistent storage methods:

[await] Storage.SaveAsync("MyKey", myObj);
[await] Storage.LoadAsync<TYPE>("MyKey");
[await] Storage.DeleteAsync("MyKey");

These methods are only available to be used asynchronously, but use the await keyword from the async/await pattern to make your life easier when programming asynchronously.

Windows 8

On Windows 8, the storage methods are limited to the persistent storage methods only, with the functionality (and code) being the same as on Windows Phone 8. The only difference here is that by default, StorageHelper will save, load and delete from the ApplicationData.Current.RoamingFolder folder, meaning everything will be persisted to the cloud and accessible on every device for a particular user. You can also opt to save, load and delete locally of course, which can be done with the overloads for each method:

[await] Storage.SaveAsync("MyKey", myObj);
[await] Storage.SaveAsync(ApplicationData.Current.LocalFolder, "MyKey", myObj);
[await] Storage.LoadAsync<TYPE>("MyKey");
[await] Storage.LoadAsync<TYPE>(ApplicationData.Current.LocalFolder, "MyKey");
[await] Storage.DeleteAsync("MyKey");
[await] Storage.DeleteAsync(ApplicationData.Current.LocalFolder, "MyKey");

Hopefully StorageHelper will help you manage the storage needs of your apps more easily and as always, let me know your feedback, comments and suggestions in the comments below or reach me on Twitter!