Sangram Nandkhile
Sangram Nandkhile

Reputation: 18202

ASP.NET - Saving in Viewstate Without serializing

I am trying to store a complex object in Viewstate to avoid calling it everytime on page load. I do understand that it needs to be serialized in order to be saved and retrived from Viewstate which means my class should be marked as [Serializable]

It's a fairly complex entity and basically i wish to avoid marking every class as Serializable as it may effect my other dependencies used for mapping.

Class Article
{
    Albums albumList { get; set; }
    Authors authorList { get; set; }
    ...
}

Is it even possible and is there any possible way out to save and retrieve the viewstate Object without Serializing?

Upvotes: 7

Views: 5484

Answers (4)

Chaitanya Gadkari
Chaitanya Gadkari

Reputation: 2807

Another possible solution is you can map all the non required properties as [NonSerialized] so that only required once will be used. I had the similar issue and went with this option.

So suppose in the above example you want Albums but not Artists then your class will become as below

[Serializable]
Class Article
{
    Albums albumList { get; set; }

    [NonSerialized]
    Authors authorList { get; set; }
    ...
}

Upvotes: 0

Ernesto
Ernesto

Reputation: 1592

It seems to me from your use case that "to avoid calling it everytime on page load" screams for cache. I think if you want it to expire maybe set some dependencies, it might work better than session (we don't know all the details).

Now, you can probably use json.net which can help you serialize your info without changing your objects. Just don't abuse viewstate, it can get nasty really fast if you let it grow. Using either session or cache (if it fits your needs) is something that can scale better in the long run.

If this is a display thing, also take a look at Output cache as maybe you can separate your repeated content in a user control or something.

All being said, I wanted to add a little example of what you actually asked using JSON.net (example stolen from them):

//Doesnt need to be marked as serializable
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };
//Use this string to save into view state
string json = JsonConvert.SerializeObject(product);
ViewState["something"]=json;

After that, get the string back from ViewState[something] and deserialize it.

string json = (string)ViewState["something"];
Product m = JsonConvert.DeserializeObject<Product>(json);

Warning, code written without compiling it.

Upvotes: 4

Special Sauce
Special Sauce

Reputation: 5604

Serialization should be avoided, if possible, because it is relatively slow (doesn't scale well).

If you are looking to cache a specific object between postbacks or across pages, use the ASP.NET Session object for session-specific caching, the Application object for site-wide caching, or use the System.Web.Caching.Cache for your own custom caching behavior. You can also cache the output of ASP.NET pages or parts of pages. More info here: ASP.NET Caching

But if you are looking to cache the whole page's viewstate, there is the option of server-side viewstate persistence. You will need to create a new BasePage in this case. For example, this is quite helpful when browsers limit the size of very large viewstates. You can get more information on this approach here: Server Side Viewstate

Upvotes: 0

Karl Anderson
Karl Anderson

Reputation: 34844

You can use ASP.NET Session object instead (this avoids the serialization route, because everything in Session is stored as an Object), like this:

var theArticle = new Article();

Session["MyArticle"] = theArticle;

Everything in Session is stored as an object so when you retrieve the Article object, then you need to cast it to an Article, like this:

var myArticleFromSession = Session["MyArticle"] as Article;

Note: It is recommended to put in checks to see if the Session key actually exists before attempting to access the value, like this:

if(Session["MyArticle"] != null)
{
    // The key MyArticle exists, so it is safe to attempt to get the object value
    var myArticleFromSession = Session["MyArticle"] as Article;

    // Since we used as to attempt the cast, 
    // then myArticleFromSession will be null if the cast failed 
    // so check for null before using myArticleFromSession
    if(myArticleFromSession != null)
    {
        // Use myArticleFromSession here

    }
}

Upvotes: 1

Related Questions