petshop 中的有些方法不太理解,觉得有些方法多余,望大家指点下
petshop 的CacheParameters和GetCachedParameters的方法是干什么?我觉得好像多余?为什么不在product.cs直接用SqlParameter就可以了,不太理解,望大家指点下。
代码如下
sqlhelper.cs
-----------------------------------------------------
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters) {
parmCache[cacheKey] = commandParameters;
}
/// <summary>
/// Retrieve cached parameters
/// </summary>
/// <param name= "cacheKey "> key used to lookup parameters </param>
/// <returns> Cached SqlParamters array </returns>
public static SqlParameter[] GetCachedParameters(string cacheKey) {
SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];
if (cachedParms == null)
return null;
SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];
for (int i = 0, j = cachedParms.Length; i < j; i++)
clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();
return clonedParms;
}
-----------------------------------------------------
product.cs
------------------------------------
StringBuilder sql = new StringBuilder(SQL_SELECT_PRODUCTS_BY_SEARCH1);
//Add each keyword to the query
for (int i = 0; i < numKeywords; i++) {
sql.Append(string.Format(SQL_SELECT_PRODUCTS_BY_SEARCH2, PARM_KEYWORD + i));