Global.asax shared/static read only property

G

Gary Bagen

If I add a shared/static read only property to the Global class of
global.asax, do I need to implement thread locking like the
Application object does?

There are two ways I want to do things, Initialize a private variable
on application_start for Web.Config value. And load the data the first
time the property is read for xml files or database loads.

I like the idea of holding static info in Web.Config/Database and
seeing that data in the rest of the app as Global.variable syntax.

Public Class Global
Inherits System.Web.HttpApplication

Private Shared sLocalTitle As String

Public Shared ReadOnly Property Title() As String
Get
'This was initialized on application_start
Return sLocalTitle
End Get
End Property

Sub Application_Start(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when the application is started
sLocalTitle = ConfigurationSettings.AppSettings("Title")
End Sub

Public Shared ReadOnly Property BookList() As DataSet
Get
Dim dsBookList As DataSet

'Is any thread locking required here? Regardless or dependant on using
the
'Cache object?
dsBookList = CType(HttpRuntime.Cache.Get("dsBookList"),
DataSet)

If dsBookList Is Nothing Then
'Cache is empty, now go get data from source

dsBookList = New DataSet

'Read from File or Database
Dim sPath As String =
HttpContext.Current.Server.MapPath("~/BookList.xml")
dsBookList.ReadXml(New StreamReader(strPath))

'Add to Cache
HttpRuntime.Cache.Insert("dsBookList", dsBookList)

End If

Return dsBookList

End Get
End Property

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top