Project DescriptionObjectStorageHelper<T> is a Generic class that simplifies storage of data in WinRT applications while still maintaining the Async principles of Windows 8 apps
OverviewWhile exploring WinRT I realised that there was a need for a way to simplify storage of data to the
RoamingFolder,
TemporaryFolder &
LocalFolder storage folders and so ObjectStorageHelper<T> was born.
With ObjectStorageHelper<T> saving and loading an object to/from disk is now just a simple method call.
I blog about ObjectStorageHelper<T> at
ObjectStorageHelperReal Windows Store Apps that are using ObjectStorageHelpermyScoreboard {"[pro|http://apps.microsoft.com/webpdp/en-us/app/myscoreboard-pro/8f4d5173-2f8a-4ff5-9f8a-99097154aaec/m/row]
BO2 Create-a-ClassMW3 Create-a-ClassCtrl-Alt-DelTestimonials"thanks for the great Generic Object Storage Helper library for WinRT. It's been a great help in version 2 of #myScoreboard"-
mjhannaf, author of app
myScoreboard"I would like to thank you for making this excellent dll, it works like a charm ;)"-
Grzegorz Ślązak, Windows 8 App Developer
"Great work. I was 50% done creating a generic utility such as this, then found yours and saved some time."-
dolowoyo, Codeplex member
RestrictionsUnder the covers ObjectStorageHelper<T> uses XML Serialization hence T needs to be an object that can be serialized as XML. There is a useful thread on Stack Overflow
Using .Net what limitations (if any) are there in using the XmlSerializer? that talks about those restrictions better than I ever could.
Saving an objectUsing ObjectStorageHelper<T> saving an object only takes two lines of code
[TestMethod]
public async Task 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
await objectStorageHelper.SaveAsync(myPoco);
}
The name of the file that gets stored is determined by the name of the type T specified in ObjectStorageHelper<T> or, if you want to specify multiple instances of T, you can specify your own file handle.
Retrieving an objectRetrieving 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();
}
Storing multiple objects of the same typeIn
V4 I
added overloads for SaveASync(), LoadAsync() & DeleteASync() to allow the user to specify a string handle for the object being saved. This allows multiple instances of type T to be saved simply by specifying different handles. I also
added a test method to ensure this is working correctly:
[TestMethod]
public async Task SaveAndLoadTwoObjectsOfSameTypeUsingDifferentHandles()
{
//Declare two instances of the same type. Notice how we only have one instance of ObjectStorageHelper<T>
var poco = new Poco() { IntProp = 1, StringProp = "one" };
var anotherPoco = new Poco() { IntProp = 2, StringProp = "two" };
var osh = new ObjectStorageHelper<Poco>(StorageType.Local);
//Store them both then retrieve them both. In each case, use a different handle.
await osh.SaveAsync(poco, handle);
await osh.SaveAsync(anotherPoco, handle2);
var result = await osh.LoadAsync(handle);
var anotherResult = await osh.LoadAsync(handle2);
//Check that the second call to SaveASync() did not overwrite the object saved by first call to SaveAsync()
Assert.AreEqual(poco, result);
Assert.AreEqual(anotherPoco, anotherResult);
}
TestsThe supplied source code contains a WinRT test library that tests various scenarios.
FeedbackFeedback is very welcome. You can get hold of me at:
jamie@removethisbit.jamie-thomson.nethttp://sqlblog.com/blogs/jamie_thomson/@jamiet