如何优化的视图状态类 [英] How to optimize class for viewstate

查看:186
本文介绍了如何优化的视图状态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个对象,我需要在ViewState中存储,它需要什么样的事情我可以做优化的大小来存储对象吗?显然,存储数据量最少需要更少的空间,但除此之外,是否有办法建筑师类,属性,attrbutes等,这将影响序列化输出有多大?

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. 我用小名字类变量,或者我用的是[ XmlAttribute (ME)]命令

  2. 我尽量不要将默认值特殊,如果都是String和大的。

  3. 我使用 [非序列化] 作为,我不拿下来存储变量。

  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.

例如,如果你从cEnaText删除默认值时,视图状态要小50%,比它。如果将[非序列化]上的所有变量,那么视图状态是空的。如果您更大的名称,然后视图状态会更大。

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

如何看实际存储是什么

有一些程序,你可以阅读视图状态,这里是一个我发现google一下。

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/

我说,你可以使用这个功能来获得所存储的,哪些不是,多少的相关信息会存储的想法。有了这一招,您可以在文本看到最后的序列化对象。

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

最后的话

您实际要求这里我们可以优化的对象和它的非常好的问题。下一步可能是对COM preSS视图状态,使其在传递给我们小。

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.

这篇关于如何优化的视图状态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆