다음 디자인에 근본적으로 잘못된 것이 있습니까? 아니면 정적 속성이 때때로 값을 잃어 버리는 이유를 알 수 있습니까?정적 속성 값이 간헐적으로 손실됩니까?
클래스 AppConfig가 포함 된 클래스 라이브러리 프로젝트가 있습니다. 이 클래스는 Webforms 프로젝트에서 사용됩니다.
다음과 같이 appconfig가 클래스의 골격은 다음과 같습니다
Public Class AppConfig
Implements IConfigurationSectionHandler
Private Const C_KEY1 As String = "WebConfig.Key.1"
Private Const C_KEY2 As String = "WebConfig.Key.2"
Private Const C_KEY1_DEFAULT_VALUE as string = "Key1defaultVal"
Private Const C_KEY2_DEFAULT_VALUE as string = "Key2defaultVal"
Private Shared m_field1 As String
Private Shared m_field2 As String
Public Shared ReadOnly Property ConfigValue1() As String
Get
ConfigValue1= m_field1
End Get
End Property
Public Shared ReadOnly Property ConfigValue2() As String
Get
ConfigValue2 = m_field2
End Get
End Property
Public Shared Sub OnApplicationStart()
m_field1 = ReadSetting(C_KEY1, C_KEY1_DEFAULT_VALUE)
m_field2 = ReadSetting(C_KEY2, C_KEY1_DEFAULT_VALUE)
End Sub
Public Overloads Shared Function ReadSetting(ByVal key As String, ByVal defaultValue As String) As String
Try
Dim setting As String = System.Configuration.ConfigurationManager.AppSettings(key)
If setting Is Nothing Then
ReadSetting = defaultValue
Else
ReadSetting = setting
End If
Catch
ReadSetting = defaultValue
End Try
End Function
Public Function Create(ByVal parent As Object, ByVal configContext As Object, ByVal section As System.Xml.XmlNode) As Object Implements System.Configuration.IConfigurationSectionHandler.Create
Dim objSettings As NameValueCollection
Dim objHandler As NameValueSectionHandler
objHandler = New NameValueSectionHandler
objSettings = CType(objHandler.Create(parent, configContext, section), NameValueCollection)
Return 1
End Function
End Class
정적 속성은 그 후 Global.asax에
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
//Fires when the application is started
AppConfig.OnApplicationStart()
End Sub
의 Application_Start 이벤트에서, 응용 프로그램 시작에 한 번 설정 얻을 때마다 우리를 어디에서든 Web.Config의 값에 액세스하려고합니다. aspx 페이지 코드 숨김 또는 다른 클래스 또는 참조 된 클래스를 호출 할 때는 단순히 정적 속성 만 호출하면됩니다.
AppConfig.ConfigValue1()
AppConfig.ConfigValue2()
이 차례가 정적 백업 필드 m_field1에 저장된 값을 반환 예를 들어
, m_field2
문제는 명확의 Web.config 항목이있는 경우이 값은 빈 문자열입니다 때때로 값.
위의 디자인에 근본적으로 잘못된 것이 있습니까? 아니면 정적 속성이 응용 프로그램 세션의 수명 동안 가치를 유지할 것으로 기대하는 것이 합리적입니까?