模版標籤主要分爲兩大類,欄目相關標籤和文章相關標籤,由於分屬於兩個表,對應不同的實體類,在替換模版標籤的時候就出現了些問題.先把枋心類貼出來,好說明問題.
string patter = @"<!--(.+?):/{(.+?)/}-->([/s/S]*?)<!--/1-->";
MatchCollection maction = Regex.Matches(tempContext, patter, RegexOptions.Compiled);
foreach (Match mac in maction)
{
labName = mac.Groups[1].Value;
labAttr = mac.Groups[2].Value;
loopStr = mac.Groups[3].Value;
Aoner.BLL.Channel channel = new Aoner.BLL.Channel();
Aoner.BLL.Article articles = new Aoner.BLL.Article();
if (labName != "page")
{
tagRow = GetAttr(labAttr, "row", true) == null ? 5 : int.Parse(GetAttr(labAttr, "row", true));
tagMode = GetAttr(labAttr, "mode", true) == null ? "stat=0" : GetAttr(labAttr, "mode", true);
tagTable = GetAttr(labAttr, "table", true) == null ? "content" : GetAttr(labAttr, "table", true);
tagWhere = GetAttr(labAttr, "where", true) == null ? "" : GetAttr(labAttr, "where", true);
tagOrder = GetAttr(labAttr, "order", true) == null ? "id desc" : GetAttr(labAttr, "order", true);
StringBuilder strSql = new StringBuilder();
strSql.Append("select *");
strSql.Append(" from ");
strSql.Append(tagTable);
strSql.Append(" where " + tagMode);
if (tagWhere != "")
strSql.Append(" and " + tagWhere);
strSql.Append(" order by " + tagOrder);
strSql.Append(" Limit " + tagRow);
//HttpContext.Current.Response.Write(strSql.ToString()); //return;
string temp = tagTable == "channel" ? Loop<Aoner.Model.Channel>(@"/[" + labName + @":(.+?)/]", loopStr, channel.TempSql(strSql.ToString()), Attribute) : Loop<mArticle>(@"/[" + labName + @":(.+?)/]", loopStr, articles.TempSql(strSql.ToString()), Attribute);
//string temp = tagTable == "channel" ? loopChannel(channel.TempSql(strSql.ToString())) : loopArticle(articles.TempSql(strSql.ToString()));
tempContext = tempContext.Replace(mac.Value, temp);
}
}
string temp = tagTable == "channel" ?
在根據表名循環替換標籤的時候,第一反應使用泛型方法:
Attribute方法兩個版本:
(1)Attribute(string pattern,string loopStr,mArticle mart)
(2)Attribute(string pattern,string loopStr,mChannel mchannel)
private string Loop<T>(string patter,string loopStr,List<T> obj)
{
string temp = string.Empty;
foreach(T t in obj)
{
temp += Attribute(string patter,loopStr,t);
}
}
還沒等高興過來,就發現編譯報錯,"T"類型不具有mArticle,mChannel兩個類的屬性,NND,泛型也不是萬能;
接下來又嘗試第二種方案:
把這兩個方法分離到兩個單獨的類中,在根據條件來實例化,功能是實現了,可代碼怎麼看怎麼便扭,兩個類的代碼百分之七八十是重複的,不爽,放棄.
這時想到了委託:
delegate string LoopReplace<T>(List<T> obj);
LoopReplace<mArticle> loopArticle = delegate(List<mArticle> marts)
{
string temps = string.Empty;
foreach (mArticle t in marts)
{
temps += Attribute(@"/[" + labName + @":(.+?)/]", loopStr, t);
}
return temps;
};
LoopReplace<Aoner.Model.Channel> loopChannel = delegate(List<Aoner.Model.Channel> marts)
{
string temps = string.Empty;
foreach (Aoner.Model.Channel t in marts)
{
temps += Attribute(@"/[" + labName + @":(.+?)/]", loopStr, t);
}
return temps;
};
string temp = tagTable == "channel" ? loopChannel(channel.TempSql(strSql.ToString())) : loopArticle(articles.TempSql(strSql.ToString()));
比起第二方案來,大碼大大減少,可還是感覺不爽,爲啥就不能像泛型那樣一個方法就足以應付各種情況呢?再次改良:
delegate string replace<T>(string pattern,string loop,T obj);
private string Loop<T>(string pattern,string loop,List<T> obj,replace<T> method)
{
string temps = string.Empty;
foreach (T t in obj)
{
temps += method(pattern, loop, t);
}
return temps;
}
string temp = tagTable == "channel" ? Loop<Aoner.Model.Channel>(@"/[" + labName + @":(.+?)/]", loopStr, channel.TempSql(strSql.ToString()), Attribute) : Loop<mArticle>(@"/[" + labName + @":(.+?)/]", loopStr, articles.TempSql(strSql.ToString()), Attribute);
這次總算是讓自己滿意了 :) 以前很少用委託,現在才發現它是這麼可愛!!!
http://www.railes.cn/zhan/547.html