Skip to main content

Client Storage in SilverLight


Client Storage for Silverlight
Silverlight provides two classes, one is IsolatedStorage and another is DataContractSerializer.

Silverlight provides IsolatedStorage class to store data at client machine. With isolated storage, data is always isolated by user and by assembly. Credentials such as the origin or the strong name of the assembly determine assembly identity. Data can also be isolated by application domain, using similar credentials.

DataContractSerializer class, which is used to serialize and deserialize instances of a type into an XML stream or document. For example, you can create a type named Person with properties that contain essential data, such as a name and address. You can then create and manipulate an instance of the Person class and write all of its property values in an XML document for later retrieval, or in an XML stream for immediate transport. Most important, the DataContractSerializer is used to serialize and deserialize data sent in Silverlight version 2 messages. Apply the DataContractAttribute attribute to classes, and the DataMemberAttribute attribute to class members to specify properties and fields that are serialized.

Problem Statement

To make use of IsolatedStorage developer has to create file stream and save the data. Ideally in real world all data will be kept in memory as an object of some type. Now to store this object in IsolatedStorage one has to first deserialize it and store it in files and define file names for them. Later this data will be read from the files and will be kept in memory again in the form of an object.

To simplify the above task I have combined the funcationality IsolatedStorage and DataContractSerializer and created ClientStorage class which helps user to store any object directly in the IsolatedStorage i.e. making object persistent an interesting feature of OOPs. Persistence is a feature of OOPs then to achieve this one must follow the oops approach rather than writing same code again and again for different objects this piece of code can be clubbed together and I have named it as ClientStorage.

What ClientStorage does:
The approach is simple and following the habit of using Session and ViewState. When we use Session to store any object in asp.net, developer does not bother where this information will be kept. She keep her objects in session when she wanted them to be available through out the life time of user's session. Same applies to IsolatedStorage, when developer wants to store something per user/application wide data on client machine that would be available cross browser to the application independent of session like persistent for always until user deletes them manually. Then why to complex things, why not something like ClientStorage[keyname] = object; and its all done.

ClientStorage is a singleton class that is basically a indexed collection which helps in fetching the objects on the basis of key. It automatically serializes the object and also stores the type of the object, so that it can be deserialize later. Gives Index based object fetching mechanism which makes life easier.

Next, it can be improved/enhanced further to provide versioning support for the objects stored in ClientStorage so that it helps in identifying the stale objects in the ClientStorage and allows to re-fetch/re-creation of that object.

I know this can be improved further, any suggestion or feedback is highly appreciable.
Source Code of Client Storage can be found here

Thanks
Rohit

Popular posts from this blog

N Tier Architecture

Some Myths People normally think if they have created UI (ASP.Net application project), BL (.Net Class Library Project), & DAL data access layer (.Net Class Library Project) and referencing these projects in their web project, they are following 3 tier architecture. I think creating a n tier application is more than this. What is tier? According to me tier means a stand-alone process which has some code to process. In the above scenario where ASP.Net Web Application is referencing BL and DAL, all the code is running in w3wp (server OS)/ aspnet_wp process which is known as worker process. So in real terms all this is single tier. Then question arises that, is it 1 tier application? If you are not dealing with databases or any other thing which has its own process then yes it is 1-tier architecture. Database makes it 2-tier architecture. As all the databases are run under some process. sqlservr is the process for SQL databases. Some people also say that a web application will always

Create Parallel Task in State Machine Workflow in SharePoint2010

I faced a problem while creating parallel tasks in state machine workflow. My requirement is same as OUT of Box SharePoint approval workflow which allows multiple users to approve the document and when everyone approves the document, document moves to next state which is approved. All the articles pointed me that this is possible in sequential workflow. Here is the thread which explains the problem, I would no be investing much time to explain the problem http://groups.google.com/group/microsoft.public.sharepoint.portalserver.development/browse_thread/thread/e0beac08110f6135 I downloaded the code sample available at Microsoft site which is basic state machine workflow. Following is the link. http://code.msdn.microsoft.com/SharePoint-2010-Creating-39ac75ea Modifications to the code are as follows: Add a new state at level 2. In the state initialization, create two tasks with different Correlation token, task ids and task properties. Now add two event