Lookup Class - ASP.NET

This page is a Draft. Its content is not complete and might contain errors.

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

Table of Contents [Hide/Show]


   TODO
   Overview and Features
   Source Code
      Lookup Base Class
         VB.NET
         C#
      CookieLookup Class
         VB.NET
         C#
      ConfigMgrLookup Class
         VB.NET
         C#
      QueryStringLookup Class
         VB.NET
         C#
      ServerVarLookup Class
         VB.NET
         C#
      SessionVarLookup Class
         VB.NET
         C#


TODO


Overview and Features

Working with ASP.NET frequently involves the use of a number of "lookups" — collections keyed on a string value — including application variables, cookies, configuration settings, query string parameters, session variables, and server variables. The following set of classes provide a consistent set of methods with which to manage the data in these lookups.


Source Code

Lookup Base Class

VB.NET

{copytext|LookupVb}
Imports System.Collections.Generic
Imports System.Reflection

Public MustInherit Class Lookup(Of EnumType)

    Protected _dict As Dictionary(Of EnumType, String)
    Protected MustOverride Function LookupValue(ByVal key As EnumType) As String
    Protected MustOverride Sub SetValue(ByVal key As EnumType, ByVal newValue As String)
    '----------------------------------------------------------------------------------------------
    Private Function TryParse(Of T)(ByVal input As String, ByRef value As T) As Boolean

        Dim result As Boolean = False
        Dim type As Type = GetType(T)
        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})), T)
            result = True
        Catch
        End Try

        Return result

    End Function
    '----------------------------------------------------------------------------------------------
    Public Function GetKey(ByVal key As EnumType) As String

        Return _dict(key)

    End Function
    '----------------------------------------------------------------------------------------------
    Public Function GetValue(ByVal key As EnumType, Optional ByVal defaultValue As String = "") _
    As String

        Dim s As String = LookupValue(key)

        If s Is Nothing Then
            s = ""
        End If

        If s.Length = 0 Then
            s = defaultValue
        End If

        Return s

    End Function
    '----------------------------------------------------------------------------------------------
    Public Function TryGetValue(Of T)(ByVal key As EnumType, ByRef value As T) As Boolean

        Return TryParse(Of T)(GetValue(key), value)

    End Function
    '----------------------------------------------------------------------------------------------
    Public Function GetValue(Of T)(ByVal key As EnumType, ByVal defaultValue As T) As T

        Dim result As T

        If TryGetValue(Of T)(key, result) Then
            Return result
        Else
            Return defaultValue
        End If

    End Function
    '----------------------------------------------------------------------------------------------
    Public WriteOnly Property [Set](ByVal key As EnumType) As String
        Set(ByVal value As String)
            Try
                SetValue(key, value)
            Catch ex As Exception
                Throw ex
            End Try
        End Set
    End Property


End Class

C#

{copytext|LookupCs}
//TODO



CookieLookup Class

VB.NET

{copytext|CookieLookupVb}
Imports System.Collections.Generic

Public Enum CookieKey
    FormVersionId
End Enum

Public Class CookieLookup
    Inherits Lookup(Of CookieKey)

    Public Sub New()

        _dict = New Dictionary(Of CookieKey, String)
        _dict.Add(CookieKey.FormVersionId, "fvid")

    End Sub

    Protected Overrides Function LookupValue(ByVal key As CookieKey) As String

        Dim result As String = ""

        Dim c As HttpCookie = HttpContext.Current.Request.Cookies(_dict(key))

        If c IsNot Nothing Then

            result = c.Value

        End If

        Return result

    End Function

    Protected Overrides Sub SetValue(ByVal key As CookieKey, ByVal newValue As String)

        Dim r As HttpResponse = HttpContext.Current.Response
        Dim name As String = _dict(key)
        Dim c As HttpCookie = r.Cookies(name)

        If c IsNot Nothing Then

            r.Cookies.Remove(name)

        End If

        c = New HttpCookie(name, newValue)
        c.Value = newValue
        c.Expires = DateTime.MaxValue
        r.Cookies.Add(c)

    End Sub

End Class

C#

{copytext|CookieLookupCs}
//TODO



ConfigMgrLookup Class

VB.NET

{copytext|ConfigMgrLookupVb}
Imports System.Collections.Generic

Public Enum ConfigMgrKey
    AppId
    LinesPerPage
    SingleSignOnMode
    AppAreaCd
    InitialRequestStatus
    InitialActionCode
End Enum
Public Class ConfigMgrLookup
    Inherits Lookup(Of ConfigMgrKey)
    '----------------------------------------------------------------------------------------------
    Public Sub New()
        _dict = New Dictionary(Of ConfigMgrKey, String)()
        _dict.Add(ConfigMgrKey.AppId, "ApplicationID")
        _dict.Add(ConfigMgrKey.LinesPerPage, "LinesPerPage")
        _dict.Add(ConfigMgrKey.SingleSignOnMode, "SSO2")
        _dict.Add(ConfigMgrKey.AppAreaCd, "AppAreaCd")
        _dict.Add(ConfigMgrKey.InitialRequestStatus, "InitialRequestStatus")
        _dict.Add(ConfigMgrKey.InitialActionCode, "InitialActionId")
    End Sub
    '----------------------------------------------------------------------------------------------
    Protected Overrides Function LookupValue(ByVal key As ConfigMgrKey) As String
        Return ConfigurationManager.AppSettings(_dict(key))
    End Function
    '----------------------------------------------------------------------------------------------
    Protected Overrides Sub SetValue(ByVal key As ConfigMgrKey, ByVal newValue As String)
        Throw New InvalidOperationException("Cannot set ConfigMgr values")
    End Sub

End Class

C#

{copytext|ConfigMgrLookupCs}
//TODO



QueryStringLookup Class

VB.NET

{copytext|QueryStringLookupVb}
Imports System.Collections.Generic

Public Enum QueryStringKey
    LanId
    IsAdmin
    TypeId
    Operation
    ErrorMessage
    SuccessNo
    EntityType
    EntityId
    CopyKeyId
    AreaCode
    RequestId
End Enum
Public Class QueryStringLookup
    Inherits Lookup(Of QueryStringKey)
    '----------------------------------------------------------------------------------------------
    Public Sub New()

        _dict = New Dictionary(Of QueryStringKey, String)()
        _dict.Add(QueryStringKey.LanId, "lid")
        _dict.Add(QueryStringKey.IsAdmin, "ia")
        _dict.Add(QueryStringKey.TypeId, "tid")
        _dict.Add(QueryStringKey.Operation, "op")
        _dict.Add(QueryStringKey.ErrorMessage, "em")
        _dict.Add(QueryStringKey.SuccessNo, "sn")
        _dict.Add(QueryStringKey.EntityType, "et")
        _dict.Add(QueryStringKey.EntityId, "eid")
        _dict.Add(QueryStringKey.CopyKeyId, "ckid")
        _dict.Add(QueryStringKey.AreaCode, "acd")
        _dict.Add(QueryStringKey.RequestId, "rid")

    End Sub
    '----------------------------------------------------------------------------------------------
    Protected Overrides Function LookupValue(ByVal key As QueryStringKey) As String

        Dim request As HttpRequest = HttpContext.Current.Request

        Dim result As String = ""

        If request IsNot Nothing Then
            result = request.QueryString(_dict(key))
        End If

        Return result

    End Function
    '----------------------------------------------------------------------------------------------
    Protected Overrides Sub SetValue(ByVal key As QueryStringKey, ByVal newValue As String)
        Throw New InvalidOperationException("Cannot set QueryStringLookup values")
    End Sub

End Class

C#

{copytext|QueryStringLookupCs}
//TODO



ServerVarLookup Class

VB.NET

{copytext|ServerVarLookupVb}
Imports System.Collections.Generic

Public Enum ServerVarKey
    UserType
    FirstName
    LastName
    EmailId
End Enum
Public Class ServerVarLookup
    Inherits Lookup(Of ServerVarKey)
    '----------------------------------------------------------------------------------------------
    Public Sub New()

        _dict = New Dictionary(Of ServerVarKey, String)()
        _dict.Add(ServerVarKey.UserType, "HTTP_USERTYPE")
        _dict.Add(ServerVarKey.FirstName, "HTTP_FIRSTNAME")
        _dict.Add(ServerVarKey.LastName, "HTTP_LASTNAME")
        _dict.Add(ServerVarKey.EmailId, "HTTP_EMAILID")

    End Sub
    '----------------------------------------------------------------------------------------------
    Protected Overrides Function LookupValue(ByVal key As ServerVarKey) As String

        Dim request As HttpRequest = HttpContext.Current.Request

        Dim result As String = ""

        If request IsNot Nothing Then
            result = request.ServerVariables(_dict(key))
        End If

        Return result

    End Function
    '----------------------------------------------------------------------------------------------
    Protected Overrides Sub SetValue(ByVal key As ServerVarKey, ByVal newValue As String)

        Dim request As HttpRequest = HttpContext.Current.Request

        If request IsNot Nothing Then
            request.ServerVariables(_dict(key)) = newValue
        Else
            Throw New InvalidOperationException("Couldn't get current Request object")
        End If

    End Sub

End Class

C#

{copytext|ServerVarLookupCs}
//TODO



SessionVarLookup Class

VB.NET

{copytext|SessionVarLookupVb}
Imports System.Collections.Generic

Public Enum SessionVarKey
    AppVerId
    LastAreaCode
End Enum
Public Class SessionVarLookup
    Inherits Lookup(Of SessionVarKey)
    '----------------------------------------------------------------------------------------------
    Public Sub New()

        _dict = New Dictionary(Of SessionVarKey, String)()
        _dict.Add(SessionVarKey.AppVerId, "AppVerId")
        _dict.Add(SessionVarKey.LastAreaCode, "LastAreaCode")

    End Sub
    '----------------------------------------------------------------------------------------------
    Protected Overrides Function LookupValue(ByVal key As SessionVarKey) As String

        Dim result As String = ""
        Dim context As HttpContext = HttpContext.Current

        If context IsNot Nothing Then
            result = context.Session(_dict(key))
        End If

        Return result

    End Function
    '----------------------------------------------------------------------------------------------
    Protected Overrides Sub SetValue(ByVal key As SessionVarKey, ByVal newValue As String)

        Dim context As HttpContext = HttpContext.Current

        If context IsNot Nothing Then
            context.Session(_dict(key)) = newValue
        Else
            Throw New InvalidOperationException("Couldn't get current Request object")
        End If

    End Sub

End Class

C#

{copytext|SessionVarLookupCs}
//TODO