How to optimize class for viewstate

前端 未结 2 563
粉色の甜心
粉色の甜心 2020-12-09 23:35

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 dat

相关标签:
2条回答
  • 2020-12-10 00:18

    you can store your viewstate on server check this out:

    Server Side Viewstate

    0 讨论(0)
  • 2020-12-10 00:29

    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.

    0 讨论(0)
提交回复
热议问题