我有以下,我想移动以下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