Settings Class - ASP.NET

This page is part of the Class Library Pages collection.
Click the icon to see the index.

This class has been deprecated by other classes in the Class Library: ConfigMgr, QueryString, ServerVar, and SessionVar.

Overview

The Settings class simplifies the management of several name-value collections in an ASP.NET website. It offers the following features.

  1. Simplified management of the following name-value collections, in which elements in the collection are referenced by an enum key rather than a string. An Initialize method specifies which of these storage locations each value is stored in.
    • Query String
    • Server Variables
    • App Settings in the web.config file
    • Session Variables
    • Cookies
  2. An Exists method, which determines whether a specified key exists, and a Missing method, which is the Boolean opposite of Exists
  3. Two overloads of the Get method: one that returns a string, and a second, generic method that parses the string into whatever datatype is specified.
  4. A Set property, to set the value of the item

Sample Implementation

Key Enum

The following code replaces the Key enum found in the file containing the Settings class.

Public Enum Key
    CurrentUser
End Enum

Settings Class Shared Constructor

Shared Sub New()
    _dict = New Dictionary(Of Key, Setting)
    Initialize(Key.CurrentUser, SettingType.ServerVariable, "AUTH_USER")
End Sub

Source Code

Settings Class

VB.NET

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

C#

TODO

Setting Class

VB.NET

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

C#

 TODO