【问题标题】:How to optimize class for viewstate如何优化视图状态的类
【发布时间】:2011-02-16 13:38:01
【问题描述】:

如果我有一个对象需要存储在视图状态中,我可以做哪些事情来优化存储对象所需的大小?显然,存储最少的数据会占用更少的空间,但除此之外,有没有办法构建类、属性、属性等,这会影响序列化输出的大小?

【问题讨论】:

    标签: asp.net serialization viewstate binary-serialization


    【解决方案1】:

    我的基本观点。

    1. 我为类和变量使用小名称,或者我使用 [XmlAttribute("ME")] 命令
    2. 我尽量不放置默认值,特别是如果它们是字符串和大的。
    3. 我使用 [NonSerialized] 来存储我没有赢得存储的变量。

    我还知道,如果我在基类中使用额外的 List,它们会占用更多空间。这是一个示例,您只需使用我提到的点就可以自己看到。

    例如,如果您从 cEnaText 中删除默认值,则视图状态将比使用它时小 50%。如果将 [NonSerialized] 放在所有变量上,则视图状态为空。如果你把名字变大,那么 viewstate 就会变大。

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

    如何查看实际存储的内容

    有一些程序可以读取视图状态,这是我在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);
    

    最后的话

    您在这里实际询问我们可以优化对象及其非常好的问题。下一步可能是压缩视图状态,使其在传输回我们时变得更小。

    【讨论】:

      【解决方案2】:

      您可以将视图状态存储在服务器上 看看这个:

      Server Side Viewstate

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-27
        • 2020-01-11
        相关资源
        最近更新 更多