Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Lookup Class - ASP.NET

RSS
Modified on Thu, Jan 14, 2010, 3:48 PM by Administrator Categorized as (drafts), ASP·NET Web Forms, ·Net Framework
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

  • Add application variables
  • Add ViewState
  • Add Pages for pages in the website

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.

  • Values are managed by an Enum rather than a string, to provide Intellisense to the developer, thus avoiding typographical errors in the names of the keys. A sample Enum is presented in the code below, and the names of the keys are initialized in a Dictionary variable in each class's constructor.
  • Values can be set via the Set method.
  • Values can be retrieved as a string via the GetValue method.
  • Values can be retrieved as other data types via the GetValue(Of T) method.
  • For fail-safe retrieval, values can be retrieved via the TryGetValue(of T) method.

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


ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2018, Patrick Jasinski.