문제

몇 가지 다른 테마를 사용할 수있는 페이지를 작성하고 있으며 각 테마에 대한 정보를 web.config에 저장하겠습니다.

새로운 섹션 그룹을 만들고 모든 것을 함께 저장하는 것이 더 효율적입니까, 아니면 모든 것을 appsettings에 넣는 것이 더 효율적입니까?

구성 솔루션

<configSections>
    <sectionGroup name="SchedulerPage">
        <section name="Providers" type="System.Configuration.NameValueSectionHandler"/>
        <section name="Themes" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
</configSections>
<SchedulerPage>
    <Themes>
        <add key="PI" value="PISchedulerForm"/>
        <add key="UB" value="UBSchedulerForm"/>
    </Themes>
</SchedulerPage>

구성의 값에 액세스하려면이 코드를 사용하고 있습니다.

    NameValueCollection themes = ConfigurationManager.GetSection("SchedulerPage/Themes") as NameValueCollection;
    String SchedulerTheme = themes["UB"];

AppSettings 솔루션

<appSettings>
    <add key="PITheme" value="PISchedulerForm"/>
    <add key="UBTheme" value="UBSchedulerForm"/>
</appSettings>

AppSettings의 값에 액세스하려면이 코드를 사용하고 있습니다.

    String SchedulerTheme = ConfigurationManager.AppSettings["UBSchedulerForm"].ToString();
도움이 되었습니까?

해결책

보다 복잡한 구성 설정의 경우 각 섹션의 역할을 명확하게 정의하는 사용자 정의 구성 섹션을 사용합니다.

<appMonitoring enabled="true" smtpServer="xxx">
  <alertRecipients>
    <add name="me" email="me@me.com"/>
  </alertRecipient>
</appMonitoring>

구성 클래스에서는 다음과 같은 것으로 속성을 노출시킬 수 있습니다.

public class MonitoringConfig : ConfigurationSection
{
  [ConfigurationProperty("smtp", IsRequired = true)]
  public string Smtp
  {
    get { return this["smtp"] as string; }
  }
  public static MonitoringConfig GetConfig()
  {
    return ConfigurationManager.GetSection("appMonitoring") as MonitoringConfig
  }
}

그런 다음 코드에서 다음 방법으로 구성 속성에 액세스 할 수 있습니다.

string smtp = MonitoringConfig.GetConfig().Smtp;

다른 팁

효율성 측면에서 측정 가능한 차이는 없습니다.

필요한 모든 것이 이름/값 쌍 만 있으면 appSettings가 좋습니다.

더 복잡한 것은 사용자 정의 구성 섹션을 작성하는 것이 좋습니다.

당신이 언급 한 예에서는 AppSettings를 사용합니다.

ConfigurationManager.AppSettings는 GetSection ( "AppSettings")만으로 성능에 차이가 없습니다. 모든 키를 열거 해야하는 경우, 모든 앱 세트를 열거하고 키에서 일부 접두사를 찾는 것보다 사용자 정의 섹션이 더 좋습니다. 그렇지 않으면 NameValueCollection보다 더 복잡한 것을 필요로하지 않는 한 appsetting을 고수하는 것이 더 쉽습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top