Saving data-objects to Isolated Storage in Silverlight

If you are developing an enterprise Silverlight application you will typically need to cache your business entities in the isolated storage. There are two apparent choices for this:

1. Use the powerful Linq-To-XML API from System.Xml.Linq namespace. This method is to be used when you want to have a fine grain control over serialization (e.g. you want to store some properties of the data objects).

2. Use the built-in DataContractSerializer class. This method is very simple and the only thing it requires is that our business objects need to have the [DataContract] and [DataMember] attributes. In this post, I am going to discuss this second method.

In an enterprise LOB (line of business) application, our business objects will typically be retrieved via WCF service and hence will already have the [DataContract] and [DataMember] attributes marked on them. So we can directly use the built-in DataContractSerializer to store them to, and later retrieve them from, the isolated storage. Here’s a generic class to simplify the process:

public static class IsolatedStorageCacheManager<T>
{
    public static void Store(string filename, T obj)
    {
        IsolatedStorageFile appStore = IsolatedStorageFile.GetUserStoreForApplication();
        using (IsolatedStorageFileStream fileStream = appStore.OpenFile(filename, FileMode.Create))
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
            serializer.WriteObject(fileStream, obj);
        }
    }
    public static T Retrieve(string filename)
    {
        T obj = default(T);
        IsolatedStorageFile appStore = IsolatedStorageFile.GetUserStoreForApplication();
        if (appStore.FileExists(filename))
        {
            using (IsolatedStorageFileStream fileStream = appStore.OpenFile(filename, FileMode.Open))
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                obj = (T)serializer.ReadObject(fileStream);
            }
        }
        return obj;
    }
}

Using the above code is really simple. Just add the class to your project and use it like this:

//for storing and retrieving a single object
Person myPersonObj = GetPerson();
IsolatedStorageCacheManager<Person>.Store("myfile.xml", myPersonObj);
Person myPersonObj2 = IsolatedStorageCacheManager<Person>.Retrieve("myfile.xml");

//for storing and retrieving a collection of objects
List<Person> myPersonList = GetPersonList();
IsolatedStorageCacheManager<List<Person>>.Store("myfile.xml", myPersonList);
List<Person> myPersonList2 = IsolatedStorageCacheManager<List<Person>>.Retrieve("myfile.xml");

Hope you will find this implementation useful.

Advertisements

11 Responses to “Saving data-objects to Isolated Storage in Silverlight”

  1. Keoz Says:

    Thanks for the post it saved my life, very elegant and clean implementation 🙂

  2. Syed Mehroz Alam Says:

    Thanks, Keoz. Glad to know that it helped you.

  3. Neil Naidoo Says:

    Nice method, works really well, thanks

  4. Tanveer Badar Says:

    As of .netfx 3.5 sp1, desktop WCF no longer requires code adoration. Don’t know what happens on the little sister.

  5. Syed Mehroz Alam Says:

    Tanveer, you are right. And here are the related MSDN Documents: Types Supported by the Data Contract Serializer and the Silverlight version documentation: DataContractSerializer

  6. Steve Says:

    Nice, thx.

  7. sqo Says:

    If you want to use with a step further IsolatedStorage you can take a look at siaqodb database engine( http://siaqodb.com ). You can save objects with one line of code and you can retrieve objects via LINQ. Is very fast, simple and easy to use.

  8. Syed Mehroz Alam Says:

    Interesting, sqo. Thanks for sharing.

  9. Ranganatha Says:

    nice stuff. This is what I was looking for.

  10. WP7 Stock Quoting Demo Series Part 3 Says:

    […] is persisted to the IsolatedStorage using a class that I borrowed from Syed Mehroz Alam at https://smehrozalam.wordpress.com/2009/02/10/saving-data-objects-to-isolated-storage-in-silverlight/ . So now we can enter one of the symbols in our Mock list such as MSFT and see the value of the […]

  11. asif Says:

    thanks. its working great


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: