How to optimize a class for viewstate

If I have an object that needs to be saved in the viewstate, what things can I do to optimize the size needed to store the object? Obviously, storing the least amount of data will take up less space, but besides this, there are ways to archive the class, properties, attrbutes, etc., which will affect how large the serialized output is?

+5
source share
3 answers

My highlights.

  • I use small names for the class and variable, or I use the command [ XmlAttribute ("ME")]
  • I try not to post default values ​​especially if they are string and large.
  • I use [NonSerialized] for variables that I did not win for storage.

I also know that if I use an extra list inside my base class, they take up much more space. Here is an example that you can see for yourself, just playing with the moments that I mention.

For example, if you delete the default value from cEnaText, the view state is 50% less than with it. If you put [NonSerialized] on all variables, the viewstate will be empty. If you make a larger name, the viewstate will be larger.

[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 find out what is actually stored

There are several programs that you can read in view mode, this is what I found on Google.

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

.

, , , , , . .

    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;
            }
        }
    }

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

, . , , , .

+3

. , , ..

, stackoverflow, .

ViewState

0

you can save your view on the server check this:

Server side view

0
source

All Articles