Results 1 to 4 of 4

Thread: Custom Configuration Handler (2.0?) - object collection

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Custom Configuration Handler (2.0?) - object collection

    Hey all!

    So I have a multiple project solution, and two of the projects are the following:

    Model
    - Contains base classes for all objects needed.

    Utility
    - Pretty much just for configuration stuff.

    Now, I'm making a multiple socket server which will be listening for both user connections as well as server connections. Via the configuration file, I'd like to make the application scalable. That is, say a new server is added, then all is necessary is to update the App.config file, this is what I'm kinda looking for:

    Code:
    <Server HostName="Socket1" IPAddress="192.168.0.10" Port="400"/>
    <Server HostName="Socket2" IPAddress="192.168.0.11" Port="400"/>
    <Server HostName="Socket3" IPAddress="192.168.0.12" Port="400"/>
    <Server HostName="Socket4" IPAddress="192.168.0.13" Port="400"/>
    Now, I'd like to have a function in the Utility project called SlaveServers that would return a generic List of servers (which is defined in the Model layer as such):

    Code:
    Imports System.Net
    Imports System.Configuration
    
    Public Class Server
        Private _hostName As String
        Private _ipAddress As String
        Private _port As Int32
    
        Public Property HostName() As String
            Get
                Return _hostName
            End Get
            Set(ByVal value As String)
                _hostName = value
            End Set
        End Property
    
        Public Property IPAddress() As String
            Get
                Return _ipAddress
            End Get
            Set(ByVal value As String)
                _ipAddress = value
            End Set
        End Property
    
        Public Property Port() As Int32
            Get
                Return _port
            End Get
            Set(ByVal value As Int32)
                _port = value
            End Set
        End Property
    
        Public Sub New()
    
        End Sub
    
        Public Sub New(ByVal inHostName As String, ByVal inIPAddress As String, ByVal inPort As Int32)
            _hostName = inHostName
            _ipAddress = inIPAddress
            _port = inPort
        End Sub
    End Class
    What is the best way to handle this? I know a configuration handler will need to be set (something like this I guess):

    Code:
    <configSections>
         <section name="SlaveServers" type="SocketServer.Utility.ConfigSectionHandler, SocketServer.Utility"/>
    </configSections>
    Where do I go from here? I am officially stumped on how to handle this.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Custom Configuration Handler (2.0?) - object collection

    Create a Servers collection class, which is basically a collection of Server objects. You could expose a method in the Servers class which allows a calling class to access all of the Server objects, possibly through an indexed property such as Servers.Items(9).

  3. #3
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Custom Configuration Handler (2.0?) - object collection

    In visual studio 2005 you can do this by creating classes that inherit from ConfigurationSection, ConfigurationElementCollection and ConfigurationElement and by marking those classes with "ConfigurationProperty" attributes.

    For your example the first thing you need is a class that represents a single row in your collection...
    VB Code:
    1. Public NotInheritable Class ServerCustomConfigurationElement
    2.         Inherits ConfigurationElement
    3.  
    4.         <ConfigurationProperty("HostName", IsRequired:=True)> _
    5.         Public Property HostName() As String
    6.             Get
    7.                 Return CStr(Me.Item("HostName"))
    8.             End Get
    9.             Set(ByVal value As String)
    10.                 Me.Item("HostName") = value
    11.             End Set
    12.         End Property
    13.  
    14.  
    15.         <ConfigurationProperty("IPAddress", IsRequired:=True)> _
    16.         Public Property IPAddress() As String
    17.             Get
    18.                 Return CStr(Me.Item("IPAddress"))
    19.             End Get
    20.             Set(ByVal value As String)
    21.                 Me.Item("IPAddress") = value
    22.             End Set
    23.         End Property
    24.  
    25.         <ConfigurationProperty("Port", IsRequired:=True)> _
    26.         Public Property Port() As String
    27.             Get
    28.                 Return CStr(Me.Item("Port"))
    29.             End Get
    30.             Set(ByVal value As String)
    31.                 Me.Item("Port") = value
    32.             End Set
    33.         End Property
    34.  
    35.     End Class

    ...then you need a class that represents a collection of them.
    The "prefered" way of setting out your collection is to have an "add", "remove" and "clear" method so your .config file would look like:
    Code:
    <Servers>
      <clear />
      <add HostName="Socket1" IPAddress="192.168.0.10" Port="400"/>
    </Servers>
    To implement this you would do:-
    VB Code:
    1. Public NotInheritable Class ServerCustomCollection
    2.         Inherits ConfigurationElementCollection
    3.  
    4.         Public Sub New()
    5.  
    6.         End Sub 'New
    7.  
    8.  
    9.         Public Overrides ReadOnly Property CollectionType() _
    10.         As ConfigurationElementCollectionType
    11.             Get
    12.                 Return ConfigurationElementCollectionType.AddRemoveClearMap
    13.             End Get
    14.         End Property
    15.  
    16.         Protected Overrides Function CreateNewElement() _
    17.         As ConfigurationElement
    18.             Return New ServerCustomConfigurationElement()
    19.         End Function 'CreateNewElement
    20.  
    21.         Protected Overrides Function GetElementKey(ByVal element _
    22.         As ConfigurationElement) As [Object]
    23.             Return CType(element, ServerCustomConfigurationElement).HostName
    24.         End Function 'GetElementKey
    25.  
    26.         Default Public Shadows Property Item( _
    27.         ByVal index As Integer) As ServerCustomConfigurationElement
    28.             Get
    29.                 Return CType(BaseGet(index), ServerCustomConfigurationElement)
    30.             End Get
    31.             Set(ByVal value As ServerCustomConfigurationElement)
    32.                 If Not (BaseGet(index) Is Nothing) Then
    33.                     BaseRemoveAt(index)
    34.                 End If
    35.                 BaseAdd(index, value)
    36.             End Set
    37.         End Property
    38.  
    39.         Default Public Shadows ReadOnly Property Item( _
    40.         ByVal Name As String) As ServerCustomConfigurationElement
    41.             Get
    42.                 Return CType(BaseGet(Name), ServerCustomConfigurationElement)
    43.             End Get
    44.         End Property
    45.  
    46.         Public Function IndexOf(ByVal ServerInstance _
    47.         As ServerCustomConfigurationElement) As Integer
    48.             Return BaseIndexOf(ServerInstance)
    49.         End Function 'IndexOf
    50.  
    51.         Public Sub Add(ByVal ServerInstanceAs ServerCustomConfigurationElement)
    52.             BaseAdd(ServerInstance)
    53.         End Sub 'Add
    54.  
    55.         Protected Overrides Sub BaseAdd(ByVal element _
    56.         As ConfigurationElement)
    57.             BaseAdd(element, False)
    58.         End Sub 'BaseAdd
    59.  
    60.         Public Overloads Sub Remove(ByVal ServerInstance _
    61.         As ServerCustomConfigurationElement)
    62.             If BaseIndexOf(ServerInstance) >= 0 Then
    63.                 BaseRemove(ServerInstance.HostName)
    64.             End If
    65.         End Sub 'Remove
    66.         Public Sub RemoveAt(ByVal index As Integer)
    67.             BaseRemoveAt(index)
    68.         End Sub 'RemoveAt
    69.  
    70.         Public Overloads Sub Remove(ByVal name As String)
    71.             BaseRemove(name)
    72.         End Sub 'Remove
    73.  
    74.         Public Sub Clear()
    75.             BaseClear()
    76.         End Sub 'Clear
    77.  
    78.     End Class

    and lastly you need a class that manages the entire section...
    VB Code:
    1. Public Class ServersCustomConfigurationSection
    2.         Inherits ConfigurationSection
    3.  
    4.         <ConfigurationProperty("Servers", _
    5.         IsDefaultCollection:=False), _
    6.         ConfigurationCollection(GetType(ServerCustomCollection), _
    7.         AddItemName:="add", _
    8.         ClearItemsName:="clear", _
    9.         RemoveItemName:="remove")> _
    10.         Public ReadOnly Property Servers() As ServerCustomCollection
    11.             Get
    12.                 Dim ServerCollection As ServerCustomCollection = _
    13.                 CType(MyBase.Item("Servers"), ServerCustomCollection)
    14.                 Return ServerCollection
    15.             End Get
    16.         End Property
    17.  
    18.     End Class

    The advantage of this set up is that you can both read and write the configuration sections at run time.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Re: Custom Configuration Handler (2.0?) - object collection

    Thanks guys, this is a huge help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width