How to optimize class for viewstate

依然范特西╮ 提交于 2019-11-27 02:56:22

问题


If I have an object I need to store in viewstate, what kinds of things can I do to optimize the size it takes to store the object? Obviously storing the least amount of data will take less space, but aside from that, are there ways to architect the class, properties, attrbutes etc, that will effect how large the serialized output is?


回答1:


My basic points.

  1. I use small names for class and variable, or I use the [XmlAttribute("ME")] command
  2. I try to not place default values especial if the are string and big ones.
  3. I use the [NonSerialized] for variables that I do not won to store.

Also I know that if I use extra List inside my base class they take a lot more space. Here is a sample that you can see by your self by just playing with the points that I mention.

For example if you remove from cEnaText the default value, the viewstate is 50% smaller than is with it. If you place the [NonSerialized] on all variables, then the viewstate is empty. If you make bigger the name, then the viewstate is going bigger.

[Serializable]
public class MyInts
{
    // this text will stored even if you never used it, Avoid to setup it here.
    public string cEnaText = "Start up text";    

    // a work around for big names, and default text.
    [XmlAttribute("TX")]
    string inside_cEnaTextWorkAroundSolution;    

    // this is not going to saved on xml.
    public string cEnaTextWorkAroundSolution;    
    {
       get
       {
         // here I return a default text that I do not store on xml
         if(string.IsNullOrWhiteSpace(inside_cEnaTextWorkAroundSolution))
            return "This is my default string that I do not won to save";
          else
            return inside_cEnaTextWorkAroundSolution;
       }
       set {inside_cEnaTextWorkAroundSolution = value;}
    } 


    // this is stored, including the class name
    public int MyInt;

    // this is not stored
    public MyInts(int getInt)
    {
        MyInt = getInt;
    }
}

[Serializable]
public class StoreMeAsTest
{
    // this is stored
    public List<MyInts> MyL = new List<MyInts>();

    // keep the name small (not like this one)
    public double cOneMoreVariable;

    // or change the xml attribute name with this command
    [XmlAttribute("ME")]
    public double cOneMoreVariableEvenBigger;

    // this is not stored
    [XmlIgnoreAttribute]
    public List<MyInts> Temporary = new List<MyInts>();


    // this is not stored
    public StoreMeAsTest()
    {
        // create some test data
        for (int i = 0; i < 100; i++)
        {
            MyL.Add(new MyInts(i));
            Temporary.Add(new MyInts(i));
        }
    }

}

public partial class Dokimes_Programming_Performance_ViewStatePerformance : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        StoreMeAsTest StoreMe = new StoreMeAsTest();

        ViewState["MyTestTable"] = StoreMe;
    }
}

How to see what actually stored

There are some programs that you can read viewstate, here is one that I found google it.

http://www.binaryfortress.com/aspnet-viewstate-helper/

How to get the idea.

I say that you can use this function to get an idea of what is stored and what is not, and how many infos are going to stored. With this trick you can see in text the final serialized object.

    public static string ObjectToXML(Type type, object obby)
    {
        XmlSerializer ser = new XmlSerializer(type);
        using (System.IO.MemoryStream stm = new System.IO.MemoryStream())
        {
            ser.Serialize(stm, obby);
            stm.Position = 0;
            using (System.IO.StreamReader stmReader = new System.IO.StreamReader(stm))
            {
                string xmlData = stmReader.ReadToEnd();
                return xmlData;
            }
        }
    }

And here how I use this function

MyLiteral.Text = ObjectToXML(typeof(StoreMeAsTest), StoreMe);

Final words

You actual ask here for we can optimize the object and its very good question. The next step is probably to compress the viewstate to make it smaller on transfer back to us.




回答2:


There are several factors to consider. You might want to give a little more information on your class, why you need to store it etc.

Old post here on stackoverflow a few optimization strategies are used.

Optimizing ViewState




回答3:


you can store your viewstate on server check this out:

Server Side Viewstate



来源:https://stackoverflow.com/questions/2784132/how-to-optimize-class-for-viewstate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!