다음과 같이 webDB의 설정을 다음과 같은 외부로 이동하려는 경우 public string method
이 예를 들어 설명하면 어떻게됩니까?메서드를 한 번 호출하는 방법
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();
을 할 수 있었지만, 미래의 코더들이 이것을 계속해야 할 때마다 인스턴스화해야한다는 사실을 알 필요가 없었습니다. 나는 그 의존성을 제거하려고한다.
완벽한 생성자 롤, 한숨 나는 웹 개발을위한 기본적인 프로그래밍 책을 다시 읽어야한다. 나는 잊어 버리고있다. !! – Anicho