Click here to Skip to main content
12,396,606 members (37,280 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads

Stats

62.4K views
1.5K downloads
68 bookmarked
Posted

Creating Custom Configurations

, 2 Sep 2004
Create isolated settings, strongly-typed objects and collections inside your web.config by leveraging the flexibility of .Net to create your own configuration sections and handler.
Imports System.Globalization

Public Class WebForm1
   Inherits System.Web.UI.Page
   Protected WithEvents list As System.Web.UI.WebControls.DropDownList
   Protected repeater As System.Web.UI.WebControls.Repeater
   Private sourceServer As Server

#Region " Web Form Designer Generated Code "
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

   End Sub
   Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
      InitializeComponent()
   End Sub
#End Region

   Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

   End Sub
   Private Sub list_SelectedIndexChange(ByVal sender As Object, ByVal e As EventArgs) Handles list.SelectedIndexChanged
      Dim type As LoadType = CType(Convert.ToInt32(list.SelectedValue, CultureInfo.InvariantCulture), LoadType)
      Dim servers As ServerCollection
      Select Case type
         Case LoadType.FirstBadWay
            servers = FirstBadMethod()
         Case LoadType.SecondBadWay
            servers = SecondBadMethod()
         Case LoadType.GoodWay
            servers = GoodMethod()
         Case Else
            Return
      End Select
      sourceServer = servers.SourceServer()
      repeater.DataSource = servers.DestinationServers()
      AddHandler repeater.ItemDataBound, AddressOf repeater_ItemDataBound
      repeater.DataBind()
   End Sub

   ' This is the first wrong way to do it. Basically you hard-code the key names in the application.
   ' This will obviously prove to be a hassle (or if you are shipping a product like this, the end of your company)
   ' when you need to add another server 
   Private Function FirstBadMethod() As ServerCollection
      Dim sc As New ServerCollection
      sc.Add(New Server(ConfigurationSettings.AppSettings("sourceServerName"), ConfigurationSettings.AppSettings("sourceServer"), True))
      sc.Add(New Server(ConfigurationSettings.AppSettings("serverName1"), ConfigurationSettings.AppSettings("server1"), False))
      sc.Add(New Server(ConfigurationSettings.AppSettings("serverName2"), ConfigurationSettings.AppSettings("server2"), False))
      sc.Add(New Server(ConfigurationSettings.AppSettings("serverName3"), ConfigurationSettings.AppSettings("server3"), False))
      sc.Add(New Server(ConfigurationSettings.AppSettings("serverName4"), ConfigurationSettings.AppSettings("server4"), False))

      'we aren't using these two, but just to show how to do it
      'notice how these aren't strongly typed and have to be cast
      Dim maxDepth As Integer = Convert.ToInt32(ConfigurationSettings.AppSettings("maxDepth"))
      Dim recursiveCopy As Boolean = Convert.ToBoolean(ConfigurationSettings.AppSettings("recursiveCopy"))

      Return sc
   End Function

   'This is the second wrong way to do it.  You basically loop and dynamically create the key name.
   'If you don't loop high enough, your code won't work properly.  But when you loop too much you get a performance hit.
   'You might be tempted to break out of the loop as soon as you don't find a hit,
   'but that's just lazy.  It's putting the responsability on your client to name things properly, because you were couldn't do it right. */
   Private Function SecondBadMethod() As ServerCollection
      Dim sc As New ServerCollection
      sc.Add(New Server(ConfigurationSettings.AppSettings("sourceServerName"), ConfigurationSettings.AppSettings("sourceServer"), True))
      For i As Integer = 0 To 100
         Dim serverAddress As String = ConfigurationSettings.AppSettings("server" + i.ToString())
         Dim serverName As String = ConfigurationSettings.AppSettings("serverName" + i.ToString())
         If Not serverAddress Is Nothing AndAlso Not serverName Is Nothing Then
            sc.Add(New Server(serverName, serverAddress, False))
         End If
      Next

      'we aren't using these two, but just to show how to do it
      'notice how these aren't strongly typed and have to be cast
      Dim maxDepth As Integer = Convert.ToInt32(ConfigurationSettings.AppSettings("maxDepth"))
      Dim recursiveCopy As Boolean = Convert.ToBoolean(ConfigurationSettings.AppSettings("recursiveCopy"))

      Return sc
   End Function

   'Beauty
   Private Function GoodMethod() As ServerCollection
      ' much more elegant, cleaner, and you get intellisense support!
      Dim maxDepth As Integer = SampleConfiguration.GetConfig.MaxDepth
      Dim recursiveCopy As Boolean = SampleConfiguration.GetConfig.RecursiveCopy
      Return SampleConfiguration.GetConfig.Servers
   End Function

   Private Sub repeater_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
      If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
         Dim lit As Literal = CType(e.Item.FindControl("sourceServer"), Literal)
         If Not lit Is Nothing Then
            lit.Text = sourceServer.Address & "&nbsp;&nbsp;(" & sourceServer.Name & ")"
         End If

      End If
   End Sub

   Private Enum LoadType
      None = 0
      FirstBadWay = 1
      SecondBadWay = 2
      GoodWay = 3
   End Enum
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Karl Seguin
Canada Canada
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 3 Sep 2004
Article Copyright 2004 by Karl Seguin
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid