Persistent Storage

Summary

PersistentStorage is an abstraction for a storage facility that allows you to store values that persists over multiple invocations of your Silverlight application. For example, you can save the user’s login name, and it’s there when they close their browser and come back the next day.

This library is dependent on the great work of Mike Talbot’s Silverlight serializer, without which, arbitrary object storage would still be a dream.

Download

Benefits of Using Persistent Storage

Using this library would benefits you in these ways:

  • Easy way to store persistent data over multiple sessions of using your application
  • Store data without worrying too much about the complexities of doing so
  • Transparent fail-over system that uses cookies if Silverlight native storage is not available
  • Obfuscation and encryption (AES) of data to prevent tampering

Sample Code

Below is some sample code to save a number of collisions, and a player name, to Persistent Storage:


using DeenGames.Utils.PersistentStorage;

// Sample initialization code
string playerName = "Deenie"; // take it from the UI
PersistentStorage.Store("player name", playerName);
PersistentStorage.Store("number of collisions", 0);
// New from 1.1: store arbitrary objects
PersistentStorage.Store("player", new Player("Ashiq"));

// To retrieve this information later
string playerName = PersistentStorage .Retrieve("player name");
int numCollisions = PersistentStorage.Retrieve("number of collisions");
Player ashiq = PersistentStorage.Retrieve("player"))

As you can tell, there’s no initialization, configuration, or setup required; just call the static functions and you’re ready to go. No type-casting or typing required.

Technical Design

PersistentStorage is an abstraction for persistent storage. Internally, it uses either Silverlight’s IsolatedStorage (for Silverlight apps) if available, or cookies (if IsolatedStorage is disabled).

It provides, for IsolatedStorage facility, hashing (to obfuscate variable names) and encryption (of values via AES); due to limited space (4kb max!), these are not used in the cookies-based version. This might change in the future.

We also use the strategy design pattern; we have a storage provider interface, and different implementations–at this point, an IsolatedStorage-based implementation (the default), and a cookie-based implementation (the backup, if IsolatedStorage is disabled).

Future Development

For future development, if there’s need, we might add functionality for the following:

  • Manage “shared” information (for non-cookie storage) that applications can save and allow other applications to access.
  • Create a desktop equivalent (that relies on Serialization) to accomplish the same functionality for desktop applications
  • Add a hash digest to every variable stored to verify that the value was not tampered with

4 Responses to Persistent Storage

  1. G says:

    The source you have published does not match the library

  2. Ashiq Alibhai, PMP says:

    @G I’ve updated the latest source again. Thanks for letting me know they were mismatched.

  3. Pierre says:

    I would recommend you reinitialize the IsolatedStorageFile isf object after calling Clear(). Because otherwise, you can no longer store new elements.

    public void Clear()
    {
    isf.Remove();
    isf = IsolatedStorageFile.GetUserStoreForApplication();
    }

    Also adding a ‘Contains’ method would be beneficial, although definitely not mandatory.

  4. Ashiq Alibhai, PMP says:

    @Pierre thanks for the recommendations. This is indeed a first-cut implementation (I’ve never even used Clear() in my own code). I will put these things on my list of todos :)

Leave a Reply

Your email address will not be published. Required fields are marked *