Intellisense For AppSettings/ConnectionStrings

One of the most time consuming things to do when starting a new web project is setting up the sites directory structure, until recently. I finally took the time to create myself a few generic templates for Visual Studio enviroment.  This has made my ramp up time on starting a new project close to nil. I have my folder stucture all set.  I created a few Classes that I typically create in all my projects, add a small library of default images and now just recently dialed the application in with jQuery wired up. Check out Sara Ford's article on making project templates in VS 2005 (Works the same in VS 2008). http://blogs.msdn.com/saraford/archive/2008/10/16/did-you-know-you-can-create-project-templates-336.aspx

 

One of the files in my project template is a class file I call SiteConfigs. This file contains all my values in my webconfig files appsettings and connectionstrings. Putting them in this file saves me the time of typing in ConfigurationManager.AppSettings("WebDomain").ToString() everytime I want to access my webconfig. It also provides me with intellisense for all my keys in my appsettings/connectionstrings. Now all I do is...

 

Response.Write("Code Version: " & SiteConfigs.CodeVersion)

 

Here's an example.

 

<WEB.CONFIG> 

 

<appSettings>

    <add key="WebUrl" value="127.0.0.1"/>

    <add key="WebDomain" value="127.0.0.1"/>

    <add key="ApplicationName" value="/"/>

    <add key="CodeVersion" value="1.0.0"/>

</appSettings>

<connectionStrings>

    <add name="SiteCS" connectionString="Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Password=" providerName="System.Data.SqlClient"/>

    <add name="ADConnectionString" connectionString=""/>

</connectionStrings>

 

<SITECONFIG.VB> 

 

Public Class SiteConfigs

 

#Region "APPSETTINGS"

 

    Public Shared ReadOnly Property WebUrl() As String

        Get

            Return ConfigurationManager.AppSettings("WebUrl").ToString()

        End Get

    End Property

 

    Public Shared ReadOnly Property WebDomain() As String

        Get

            Return ConfigurationManager.AppSettings("WebDomain").ToString()

        End Get

    End Property

 

    Public Shared ReadOnly Property ApplicationName() As String

        Get

            Return ConfigurationManager.AppSettings("ApplicationName").ToString()

        End Get

    End Property

 

    Public Shared ReadOnly Property CodeVersion() As String

        Get

            Return ConfigurationManager.AppSettings("CodeVersion").ToString()

        End Get

    End Property

#End Region

 

#Region "CONNECTIONSTRINGS"

 

    Public Shared ReadOnly Property SiteCS() As String

        Get

            Return ConfigurationManager.ConnectionStrings("SiteCS").ToString()

        End Get

    End Property

 

    Public Shared ReadOnly Property ADConnectionString() As String

        Get

            Return ConfigurationManager.ConnectionStrings("ADConnectionString").ToString()

        End Get

    End Property

 

#End Region

 

End Class

Published Friday, December 26, 2008 8:33 AM by rojay12

Comments

# re: Intellisense For AppSettings/ConnectionStrings

Friday, December 26, 2008 7:21 PM by Robert McLaws

That's a great idea. You should make it a "My" namespace mod and drop it in there, so your code would look like "My.SiteConfig.ConnectionString" or "My.SiteConfig.Version". Seems like it would be prety useful.

# re: Intellisense For AppSettings/ConnectionStrings

Saturday, December 27, 2008 9:06 AM by Steve Sheldon

I started doing something similar a few years ago and I agree that it makes life easier.

# re: Intellisense For AppSettings/ConnectionStrings

Thursday, February 05, 2009 9:20 AM by Ray Clanan

I think this is a wonderful idea. Do you plan on releasing your template to help us get an idea of a good starting point? Or at least detail it a little more?

Thanks for the post.

Leave a Comment

(required) 
(required) 
(optional)
(required)