我有以下,我想移動以下public string method
以外的webDB和項目的設置,這是一個例子,我將如何去做這件事。一種方法調用方法一次
public string Width
{
get
{
if (webDB != null)
{
webDB = Sitecore.Configuration.Factory.GetDatabase("web");
Sitecore.Data.Items.Item item = webDB.Items[StartItem];
if (item != null)
{
Sitecore.Data.Fields.Field field = item.Parent.Fields["Identity_Page_Width"];
if (!String.IsNullOrEmpty(field.Value))
{
return field.Value;
}
else
{
return "964"; // returns default pixel width if Identity_Page_Width is not defined, or is null
}
}
else
{
return "964"; // If item is not found return default width.
}
}
else
{
return "964";
}
}
}
這是怎麼我試圖分開吧:
public void GetConfiguration()
{
if (webDB != null)
{
webDB = Sitecore.Configuration.Factory.GetDatabase("web");
if (item != null)
{
item = webDB.Items[StartItem];
}
}
}
,但我會被卡住,試圖運行代碼我得到method must have a return type
內的方法。
然後我想在此類的某個地方只運行此GetConfiguration,因此所有方法都不需要聯繫數據庫和項目數據就可以了。
我可以做MyClass class = New MyClass; Class.GetConfiguration();
但我不希望未來的編碼人員必須知道這需要每次都要實例化以繼續。我寧願刪除這種依賴。
完美的構造函數哈哈,嘆了口氣我應該重新閱讀基本的編程書IDE的Web開發正在讓我忘記了基本知識。 – Anicho 2012-01-16 14:46:06