我的ASP.NET應用程序讀取一個xml文件以確定它當前處於哪個環境(例如本地,開發,生產)。從物理文件讀取連接字符串vs應用程序對象(ASP.NET)的優點/缺點?
它每次打開一個到數據庫的連接都會檢查這個文件,以便知道從應用程序設置中獲取哪個連接字符串。
我正在進入發展階段,效率正在成爲一個問題。我不認爲在我希望訪問數據庫的情況下(非常頻繁)必須在物理磁盤上讀取文件是一個好主意。
我正在考慮在Application [「ConnectionString」]中存儲連接字符串。因此,代碼將
public static string GetConnectionString
{
if (Application["ConnectionString"] == null)
{
XmlDocument doc = new XmlDocument();
doc.Load(HttpContext.Current.Request.PhysicalApplicationPath + "bin/ServerEnvironment.xml");
XmlElement xe = (XmlElement) xnl[0];
switch (xe.InnerText.ToString().ToLower())
{
case "local":
connString = Settings.Default.ConnectionStringLocal;
break;
case "development":
connString = Settings.Default.ConnectionStringDevelopment;
break;
case "production":
connString = Settings.Default.ConnectionStringProduction;
break;
default:
throw new Exception("no connection string defined");
}
Application["ConnectionString"] = connString;
}
return Application["ConnectionString"].ToString();
}
我沒有設計的應用程序,這樣我想一定有一個原因,每一次讀取XML文件(更改設置的應用程序運行時?)我很少概念這裏的內部運作。優缺點都有什麼?你認爲通過實現上述功能我會看到一個小的性能增益嗎?
感謝
如果有理由保留XML文件,您可以隨時將解析後的文檔添加到緩存中,以消除可能來自其的任何性能問題。而且,這不會增加太多的代碼。另外,如果您還在文件(包含的功能)上創建緩存依賴項,則緩存項目將在文件更改後自動過期。 – 2010-04-09 17:06:55