Best way to load/store web site settings in database

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

This is the best method I've found to store and load settings from database.
I load the settings once at start as public variables. The method can be
called easily anytime you want to load the new settings from database.
Comments very welcome... I'd like to refine this... besides the error
handling I need to add for the db conn...

Global.asax ---------------
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim oAppConfig As New myWebAppName.AppConfig
oAppConfig.LoadSettings()
End Sub

AppConfig Class -------------------
Public Class AppConfig
Public Sub LoadSettings()
Dim strConnectionString = "hardcode here or use the web.config"

'some db code here...
AppVars.strMailTo = 'get from database code here
AppVars.strCompanyName = 'get from database code here
AppVars.strSomeOtherVar = 'get from database code here
'and so on...
End Sub
Public Sub StoreSettings()
'same idea here...
End Sub
End Class

AppVars Module - still debating on whether to use public class or module
since they are the same thing...
----------------------
Module AppVars
Public strMailTo
Public strCompanyName
Public strSomeOtherVar
End Module


-Max
 
Max said:
This is the best method I've found to store and load settings from database.
I load the settings once at start as public variables. The method can be
called easily anytime you want to load the new settings from database.
Comments very welcome... I'd like to refine this... besides the error
handling I need to add for the db conn...

Global.asax ---------------
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim oAppConfig As New myWebAppName.AppConfig
oAppConfig.LoadSettings()
End Sub

AppConfig Class -------------------
Public Class AppConfig
Public Sub LoadSettings()
Dim strConnectionString = "hardcode here or use the web.config"

'some db code here...
AppVars.strMailTo = 'get from database code here
AppVars.strCompanyName = 'get from database code here
AppVars.strSomeOtherVar = 'get from database code here
'and so on...
End Sub
Public Sub StoreSettings()
'same idea here...
End Sub
End Class

AppVars Module - still debating on whether to use public class or module
since they are the same thing...
----------------------
Module AppVars
Public strMailTo
Public strCompanyName
Public strSomeOtherVar
End Module


Best way ... to me this sounds like you should be using the Session object to
store the values instead of your own class. You can use your own class, but it
goes out of scope after every page load while the Session object does not.

Mythran
 
But my way is in the application scope. A public class makes variables
available to all users on the web site. As long as I only store a few
strings -- 20 or 30, it should not use many resources. But your idea, in the
session state, every user would have their own set of variables created,
which would use a lot more memory!

Plus if I wanted to change the web site's settings, I want the new settings
to be available to all users currently on my site, without having every
session load up new variables, and without the application restarting
(losing everyone's session entirely).

Am I right? Is there a better way? I think not. ;)

-Max
 
Back
Top