Settings
Initialize
Exists
Missing
Get
Set
Key
Public Enum Key CurrentUser End Enum
Shared Sub New() _dict = New Dictionary(Of Key, Setting) Initialize(Key.CurrentUser, SettingType.ServerVariable, "AUTH_USER") End Sub
Imports Microsoft.VisualBasic Imports System.Collections.Generic Imports System.Reflection Public Enum Key CurrentUser End Enum Public Class Settings Private Shared _dict As Dictionary(Of Key, Setting) Shared Sub New() _dict = New Dictionary(Of Key, Setting) Initialize(Key.CurrentUser, SettingType.ServerVariable, "AUTH_USER") End Sub Public Shared ReadOnly Property Exists(ByVal key As Key) As Boolean Get Try Dim result As Boolean = True If Not _dict.ContainsKey(key) Then result = False ElseIf _dict(key) Is Nothing Then result = False ElseIf _dict(key).Value Is Nothing Then result = False ElseIf _dict(key).Value.Length = 0 Then result = False End If Return result Catch ex As Exception Throw ex End Try End Get End Property Public Shared ReadOnly Property Missing(ByVal key As Key) As Boolean Get Try Return Not Exists(key) Catch ex As Exception Throw ex End Try End Get End Property Public Shared Sub Initialize(ByVal key As Key, ByVal type As SettingType) Try _dict.Add(key, New Setting(key.ToString(), type)) Catch ex As Exception Throw ex End Try End Sub Public Shared Sub Initialize(ByVal key As Key, ByVal type As SettingType, ByVal name As String) Try _dict.Add(key, New Setting(name, type)) Catch ex As Exception Throw ex End Try End Sub Public Shared Function [Get](ByVal key As Key) As String Try Dim result As String = "" If Exists(key) Then result = _dict(key).Value End If If result Is Nothing Then result = "" End If Return result Catch ex As Exception Throw ex End Try End Function Public Shared Function [Get](Of T)(ByVal key As Key) As T Try Dim result As T = Nothing If Exists(key) Then _dict(key).TryParse(Of T)(result) End If Return result Catch ex As Exception Throw ex End Try End Function Public Shared Function GetName(ByVal key As Key) As String Try Dim result As String = "" If Exists(key) Then result = _dict(key).Name End If Return result Catch ex As Exception Throw ex End Try End Function Public Shared WriteOnly Property [Set](ByVal key As Key) As String Set(ByVal value As String) Try If Exists(key) Then _dict(key).Value = value End If Catch ex As Exception Throw ex End Try End Set End Property End Class
TODO
Imports Microsoft.VisualBasic Imports System.Reflection Public Enum SettingType QueryString ServerVariable AppSetting SessionVariable Cookie End Enum Public Class Setting Private _name As String Private _type As SettingType Public Sub New(ByVal name As String, ByVal type As SettingType) Dim x As Integer = Nothing _name = name _type = type End Sub Private ReadOnly Property NVC() As NameValueCollection Get Dim result As NameValueCollection = Nothing Select Case _type Case SettingType.AppSetting result = ConfigurationManager.AppSettings Case SettingType.ServerVariable result = HttpContext.Current.Request.ServerVariables Case SettingType.QueryString result = HttpContext.Current.Request.QueryString Case SettingType.Cookie, SettingType.SessionVariable result = Nothing End Select Return result End Get End Property Public ReadOnly Property Name() As String Get Return _name End Get End Property Public ReadOnly Property Type() As SettingType Get Return _type End Get End Property Public Property Value() As String Get Try Dim result As String = "" If NVC IsNot Nothing Then result = NVC(_name) If result Is Nothing Then result = "" End If ElseIf _type = SettingType.Cookie Then If HttpContext.Current.Request.Cookies(_name) IsNot Nothing Then result = HttpContext.Current.Request.Cookies(_name).Value End If ElseIf _type = SettingType.SessionVariable Then If HttpContext.Current.Session(_name) IsNot Nothing Then result = HttpContext.Current.Session(_name).ToString() End If End If Return result Catch ex As Exception Throw ex End Try End Get Set(ByVal value As String) If NVC IsNot Nothing Then NVC(_name) = value.ToString() ElseIf _type = SettingType.Cookie Then Dim request As HttpRequest = HttpContext.Current.Request Dim cookie As HttpCookie = request.Cookies.Item(_name) If cookie Is Nothing Then cookie = New HttpCookie(_name) request.Cookies.Add(cookie) End If cookie.Value = value.ToString() ElseIf _type = SettingType.SessionVariable Then HttpContext.Current.Session(_name) = value End If End Set End Property Public Function TryParse(Of DataType)(ByRef value As DataType) As Boolean Return TryParse(Me.Value, value) End Function Public Shared Function TryParse(Of DataType)(ByVal input As String, ByRef value As DataType) As Boolean Dim result As Boolean = False Dim type As Type = GetType(DataType) Dim pt As Type() = New Type() {GetType(System.String)} Dim mi As MethodInfo = type.GetMethod("Parse", pt) value = Nothing Try value = DirectCast((mi.Invoke(Nothing, New Object() {input})), DataType) result = True Catch End Try Return result End Function End Class