This is a migrated thread and some comments may be shown as answers.

What should LoadStateFromStorage return when empty?

5 Answers 113 Views
Persistence Framework
This is a migrated thread and some comments may be shown as answers.
gpe
Top achievements
Rank 1
gpe asked on 04 Mar 2014, 09:05 AM
I'm using the clean sample of a CookieStateStorageProvider from http://demos.telerik.com/aspnet-ajax/persistence-framework/examples/custom-storage-provider/defaultcs.aspx

When the CookieStateStorageProvider tries is called upon a key it does not know, returning null leads to an exception in the PersistenceManager. The thing is that I don't want my view to have to check if this key is already existing or not. It makes more sense that only the CookieStateStorageProvider knows about how to store the state. But what should its method return when the key is not valid/existing?

I tried returning null or String.Empty but none is appreciated my the PersistenceManager.

5 Answers, 1 is accepted

Sort by
0
Dobromir
Telerik team
answered on 05 Mar 2014, 10:21 AM
Hi,

I have already answered your support ticket on the subject. For convenience I will paste my answer here as well.

By design, the PersistenceManager expects the storage provider's LoadStateFromStorage method to return a string acceptable for the serialization provider used by the manager - the default serialization provider used is XmlStateSerializer, thus in this case the returned value should be in XML format.

If I understand you correctly, you want to move the check if the cookie exists inside the StorageProvider. To do so, I would suggest you throw an exception from within the LoadStateFromStorage method instead of returning a dummy result.

In addition, when working with Cookies as state storage, it is highly recommended to combine StateStorageProvider with appropriate StateSerializationProvider, please check out the following demo providing BinarySerializer:
http://demos.telerik.com/aspnet-ajax/persistence-framework/examples/custom-serializer-provider/defaultcs.aspx


Regards,
Dobromir
Telerik

DevCraft Q1'14 is here! Join the free online conference to see how this release solves your top-5 .NET challenges. Reserve your seat now!

0
Per
Top achievements
Rank 1
answered on 22 Sep 2014, 12:05 PM
Hi

There is a bug in the example.
Replace "LoadStateFromStorage" with this and it will work...

    public string LoadStateFromStorage(string key)
    {
      if (HttpContext.Current.Request.Cookies[StorageKey] == null)
        throw new PersistenceFrameworkStorageException("Unable to read cookie storage content. ");

      return DecompressString(HttpContext.Current.Request.Cookies[StorageKey].Value.ToString());
    }

Regards
Per
0
Vessy
Telerik team
answered on 25 Sep 2014, 11:37 AM
Hi Per,

Thank you for your observation - we will update the example right away.

As a sign of gratitude we have also updated your Telerik points accordingly.

Regards,
Vessy
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Gary
Top achievements
Rank 1
answered on 09 Apr 2015, 10:39 AM

The example has still not been updated.

Gary

0
Daniel
Telerik team
answered on 14 Apr 2015, 12:12 PM
Hello Gary,

Thank you for your suggestion. I'm afraid we could not modify the live demo to throw an exception.
Also, we have an explicit check on the click of the Load button here:
protected void LoadButton_Click(object sender, EventArgs e)
{
    if (Request.Cookies[CookieName] != null)
    {
        RadPersistenceManager1.LoadState();
        RadGrid1.Rebind();
    }
}

As to the case you mentioned, it is possible to avoid the exception inside the state serialization provider if you have one. It would be something like this:
public class YourStateSerializer : IStateSerializer
{
         public List<RadControlState> DeserializeCollection(string stateData)
         {
              if (String.IsNullOrEmpty(stateData))
              {
                       return new List<RadControlState>();
              }
    ...

This will work if you return an empty string from the LoadStateFromStorage method in the storage provider.

I hope this helps.

Regards,
Daniel
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
Persistence Framework
Asked by
gpe
Top achievements
Rank 1
Answers by
Dobromir
Telerik team
Per
Top achievements
Rank 1
Vessy
Telerik team
Gary
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or