基于泛型的延时加载集合实现代码

本文介绍了一种泛型 LazyLoadList 的实现方式,该类实现了 IList 接口,能够延迟加载数据。当首次访问数据时,它会从数据库中加载数据到内存,并缓存这些数据供后续使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ContractedBlock.gifExpandedBlockStart.gif
None.gif    [Serializable]
None.gif    
public class LazyLoadList<T> : IList<T>, System.Collections.IList,ISetLoadInfo
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
private IList<T> mItems = null;
InBlock.gif        
protected IList<T> Items
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (mItems == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    LoadData();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return mItems;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void LoadData()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (Driver == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                mItems 
= new List<T>();
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
using (HFSoft.Data.IDataSession session =
InBlock.gif                HFSoft.Data.MappingContainer.ConfigContainer.OpenSession(Driver, ConnectionString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                session.Open();
InBlock.gif                IQuery query 
= Relation.GetRightQuery(session,Expression);
InBlock.gif                mItems 
= query.List<T>();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IList 成员#region IList<T> 成员
InBlock.gif
InBlock.gif        
int IList<T>.IndexOf(T item)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Items.IndexOf(item);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void IList<T>.Insert(int index, T item)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Items.Insert(index, item);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void IList<T>.RemoveAt(int index)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Items.RemoveAt(index);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        T IList
<T>.this[int index]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Items[index];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Items[index] 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ICollection 成员#region ICollection<T> 成员
InBlock.gif
InBlock.gif        
void ICollection<T>.Add(T item)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Items.Add(item);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void ICollection<T>.Clear()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Items.Clear();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
bool ICollection<T>.Contains(T item)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Items.Contains(item);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Items.CopyTo(array, arrayIndex);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
int ICollection<T>.Count
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn Items.Count; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
bool ICollection<T>.IsReadOnly
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn ((ICollection<T>)Items).IsReadOnly; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
bool ICollection<T>.Remove(T item)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Items.Remove(item);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IEnumerable 成员#region IEnumerable<T> 成员
InBlock.gif
InBlock.gif        IEnumerator
<T> IEnumerable<T>.GetEnumerator()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Items.GetEnumerator();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IEnumerable 成员#region IEnumerable 成员
InBlock.gif
InBlock.gif        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Items.GetEnumerator();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IList 成员#region IList 成员
InBlock.gif
InBlock.gif        
int System.Collections.IList.Add(object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ((IList)Items).Add((T)value);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void System.Collections.IList.Clear()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Items.Clear();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
bool System.Collections.IList.Contains(object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Items.Contains((T)value);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
int System.Collections.IList.IndexOf(object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Items.IndexOf((T)value);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void System.Collections.IList.Insert(int index, object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Items.Insert(index, (T)value);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
bool System.Collections.IList.IsFixedSize
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn ((IList)Items).IsFixedSize; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
bool System.Collections.IList.IsReadOnly
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn ((IList)Items).IsReadOnly; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void System.Collections.IList.Remove(object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Items.Remove((T)value);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void System.Collections.IList.RemoveAt(int index)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Items.RemoveAt(index);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
object System.Collections.IList.this[int index]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Items[index];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Items[index] 
= (T)value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ICollection 成员#region ICollection 成员
InBlock.gif
InBlock.gif        
void System.Collections.ICollection.CopyTo(Array array, int index)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ((IList)Items).CopyTo(array, index);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
int System.Collections.ICollection.Count
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn Items.Count; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
bool System.Collections.ICollection.IsSynchronized
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn ((IList)Items).IsSynchronized; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
object System.Collections.ICollection.SyncRoot
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn ((IList)Items).SyncRoot; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        [NonSerialized]
InBlock.gif        
internal HFSoft.Data.IDriver Driver;
InBlock.gif        [NonSerialized]
InBlock.gif        
internal string ConnectionString;
InBlock.gif        [NonSerialized]
InBlock.gif        
internal HFSoft.Data.Expressions.Expression Expression;
InBlock.gif        [NonSerialized]
InBlock.gif        
internal RelationAttribute Relation;
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ISetLoadInfo 成员#region ISetLoadInfo 成员
InBlock.gif
InBlock.gif        
void ISetLoadInfo.SetInfo(HFSoft.Data.IDriver driver, string connstring, HFSoft.Data.Expressions.Expression expression, RelationAttribute relation)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Driver 
= driver;
InBlock.gif            ConnectionString 
= connstring;
InBlock.gif            Expression 
= expression;
InBlock.gif            Relation 
= relation;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

None.gif    
interface ISetLoadInfo
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
void SetInfo(HFSoft.Data.IDriver driver, string connstring, HFSoft.Data.Expressions.Expression expression,RelationAttribute relation);
ExpandedBlockEnd.gif    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值