Is there something fundamentally wrong with the next design, or can someone understand why static properties sometimes lose their meaning?
I have a class library project containing an AppConfig class; this class is used by the Webforms project.
The skeleton of the AppConfig class is as follows:
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
Static properties are set once at application startup, from the Application_Start event for Global.asax
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
AppConfig.OnApplicationStart()
End Sub
After that, whenever we want to access a value in Web.Config from anywhere, for example. aspx page code-behind or another class or reference class, we just call the static property.
For instance,
AppConfig.ConfigValue1()
AppConfig.ConfigValue2()
This rotation returns the value stored in the static backup fields m_field1, m_field2
, , Web.Config .
- , , ?