|
-
Jun 1st, 2006, 01:26 PM
#1
Thread Starter
Junior Member
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.
-
Jun 1st, 2006, 04:15 PM
#2
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).
-
Jun 2nd, 2006, 03:16 AM
#3
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:
Public NotInheritable Class ServerCustomConfigurationElement
Inherits ConfigurationElement
<ConfigurationProperty("HostName", IsRequired:=True)> _
Public Property HostName() As String
Get
Return CStr(Me.Item("HostName"))
End Get
Set(ByVal value As String)
Me.Item("HostName") = value
End Set
End Property
<ConfigurationProperty("IPAddress", IsRequired:=True)> _
Public Property IPAddress() As String
Get
Return CStr(Me.Item("IPAddress"))
End Get
Set(ByVal value As String)
Me.Item("IPAddress") = value
End Set
End Property
<ConfigurationProperty("Port", IsRequired:=True)> _
Public Property Port() As String
Get
Return CStr(Me.Item("Port"))
End Get
Set(ByVal value As String)
Me.Item("Port") = value
End Set
End Property
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:
Public NotInheritable Class ServerCustomCollection
Inherits ConfigurationElementCollection
Public Sub New()
End Sub 'New
Public Overrides ReadOnly Property CollectionType() _
As ConfigurationElementCollectionType
Get
Return ConfigurationElementCollectionType.AddRemoveClearMap
End Get
End Property
Protected Overrides Function CreateNewElement() _
As ConfigurationElement
Return New ServerCustomConfigurationElement()
End Function 'CreateNewElement
Protected Overrides Function GetElementKey(ByVal element _
As ConfigurationElement) As [Object]
Return CType(element, ServerCustomConfigurationElement).HostName
End Function 'GetElementKey
Default Public Shadows Property Item( _
ByVal index As Integer) As ServerCustomConfigurationElement
Get
Return CType(BaseGet(index), ServerCustomConfigurationElement)
End Get
Set(ByVal value As ServerCustomConfigurationElement)
If Not (BaseGet(index) Is Nothing) Then
BaseRemoveAt(index)
End If
BaseAdd(index, value)
End Set
End Property
Default Public Shadows ReadOnly Property Item( _
ByVal Name As String) As ServerCustomConfigurationElement
Get
Return CType(BaseGet(Name), ServerCustomConfigurationElement)
End Get
End Property
Public Function IndexOf(ByVal ServerInstance _
As ServerCustomConfigurationElement) As Integer
Return BaseIndexOf(ServerInstance)
End Function 'IndexOf
Public Sub Add(ByVal ServerInstanceAs ServerCustomConfigurationElement)
BaseAdd(ServerInstance)
End Sub 'Add
Protected Overrides Sub BaseAdd(ByVal element _
As ConfigurationElement)
BaseAdd(element, False)
End Sub 'BaseAdd
Public Overloads Sub Remove(ByVal ServerInstance _
As ServerCustomConfigurationElement)
If BaseIndexOf(ServerInstance) >= 0 Then
BaseRemove(ServerInstance.HostName)
End If
End Sub 'Remove
Public Sub RemoveAt(ByVal index As Integer)
BaseRemoveAt(index)
End Sub 'RemoveAt
Public Overloads Sub Remove(ByVal name As String)
BaseRemove(name)
End Sub 'Remove
Public Sub Clear()
BaseClear()
End Sub 'Clear
End Class
and lastly you need a class that manages the entire section...
VB Code:
Public Class ServersCustomConfigurationSection
Inherits ConfigurationSection
<ConfigurationProperty("Servers", _
IsDefaultCollection:=False), _
ConfigurationCollection(GetType(ServerCustomCollection), _
AddItemName:="add", _
ClearItemsName:="clear", _
RemoveItemName:="remove")> _
Public ReadOnly Property Servers() As ServerCustomCollection
Get
Dim ServerCollection As ServerCustomCollection = _
CType(MyBase.Item("Servers"), ServerCustomCollection)
Return ServerCollection
End Get
End Property
End Class
The advantage of this set up is that you can both read and write the configuration sections at run time.
-
Jun 2nd, 2006, 01:48 PM
#4
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|