假設我有對象ViewState的 - 適當的方式存儲在數據庫數據
List<MyClass> collection = new List<MyClass>();
以及這些對象將被插入
Dictionary<string, List<MyClass>> sections = new Dictionary<string, List<MyClass>>();
// example of insertion
sections["A"].Add(collection[2]);
列表和字典存儲在ViewState
字典的集合。在需要時,將字典中的對象的標識符存儲起來並將它們與對象綁定在一起時,它會更符合內存嗎?
Dictionary<string, List<int>> sections = new Dictionary<string, List<int>>();
// example of insertion
sections["A"].Add(collection[2].ID);
據我所知,沒有性能提升,因爲在第一種情況下,字典將包含大小爲32/64位的引用。這是真的嗎?
EDIT
MyClass
是一類,而不是一個結構。 列表和字典的聲明如下:
private List<MyClass> A
{
get
{
if (ViewState["a"] == null)
{
ViewState["a"] = // retrieve data from db
}
return (List<MyClass>)ViewState["a"];
}
set
{
ViewState["a"] = value;
}
}
private Dictionary<string, List<MyClass>> B
{
get
{
if (ViewState["b"] == null)
{
ViewState["b"] = new Dictionary<string, List<MyClass>>();
}
return (Dictionary<string, List<MyClass>>)ViewState["b"];
}
set
{
ViewState["b"] = value;
}
}
是的,如果'MyClass'是'class',而不是'struct' – 2013-03-27 14:49:42
你在哪裏保留你的對象?在您的頁面發送給客戶端後,它們將被銷燬,因此即使您在視圖狀態中保留對它們的引用,它們也不會指向您回程中的任何有效內容。所以,你很可能需要將它們保持在會話中,並且一旦你將它們放在會話中,就沒有必要在視圖狀態中有任何內容引用它們。 – AaronS 2013-03-27 14:56:34
修改我的帖子。 – ChruS 2013-03-27 15:08:09