应用程序配置文件中的嵌套元素






4.44/5 (6投票s)
学习如何使用 .NET 配置库在 app.config 文件中构建嵌套元素
引言
使用配置文件是修改应用程序行为而无需更改内部代码的关键方面。在 .NET 中,有强大的功能来管理 XML 格式的设置文件。使用由键值对组成的应用程序设置非常简单! 当你尝试为你的业务逻辑实现自定义配置文件时,问题就出现了。 在本文中,我介绍了一种在单独的配置文件中构建嵌套元素的方法。
外部文件
为了为应用程序的不同部分创建不同的配置,我将以下代码添加到app.config 中
<sectionGroup name="customSectionGroup" type="ecoLDS_HMI.CustomConfigSectionGroup,
ecoLDS_HMI">
<section name="customUserSettings" type="ecoLDS_HMI.CustomConfigSection, ecoLDS_HMI"/>
</sectionGroup>
<customSectionGroup >
<customUserSettings configSource="UserSettings.config" />
</customSectionGroup>
通过指定配置源,自定义用户设置存储在一个名为UserSettings.config 的单独文件中。
自定义 .config 文件
在UserSetting.config 文件中,我存储了我的自定义配置,该配置由 zooms
节点作为根元素以及 zoomLine
节点的序列组成,其中包含 zoomZone
的序列。 我使用这种类型的配置文件来根据线路的结构为我的地图组件设置不同的缩放级别。
<customUserSettings userName="Enrico" expiresOn="10/10/2017" >
<zooms>
<zoomLine numLinea="1">
<zones>
<zoomZone numNode="1" zoomValue="200"/>
<zoomZone numNode="5" zoomValue="300"/>
</zones>
</zoomLine>
<zoomLine numLine="2" >
<zones>
<zoomZone numNode="1" zoomValue="200"/>
<zoomZone numNode="2" zoomValue="200"/>
</zones>
</zoomLine>
</zooms>
</customUserSettings>
实现
为了读取我的配置文件UserSetting.config,我利用了 .NET Framework 的 System.Configuration
库,扩展了类 ConfigurationSection
、ConfigurationElementCollection
和 ConfigurationElement
。
为了读取由节点 customUserSettings
表示的部分,该部分具有两个属性 userName
和 expiresOn
, 我扩展了类 ConfigurationSection
,为每个属性和子元素 zooms
添加一个配置属性。
Public Class CustomConfigSection
Inherits ConfigurationSection
Public Sub New()
End Sub
<ConfigurationProperty("userName")> _
Public Property UserName() As String
Get
Return CType(Me("userName"), String)
End Get
Set(ByVal value As String)
Me("userName") = value
End Set
End Property
<ConfigurationProperty("expiresOn")> _
Public Property ExpiresOn() As String
Get
Return CType(Me("expiresOn"), String)
End Get
Set(ByVal value As String)
Me("expiresOn") = value
End Set
End Property
<ConfigurationProperty("zooms")> _
Public ReadOnly Property CustomConfigElementCollection() As ZoomLineCollection
Get
Return TryCast(Me("zooms"), ZoomLineCollection)
End Get
End Property
End Class
最后一个配置属性 zooms
包含一个 ZoomLine
元素列表,该列表收集在一个名为 ZoomLineCollection
的集合中。
用于读取 ZoomLine
元素的类扩展了 ConfiguarationElement
类。 我们为每个属性添加了一个配置属性,并按照之前的模式为元素 zones
添加了一个配置属性。
zones
配置属性包含一个 zone
元素集合。
Public Class ZoomLine
Inherits ConfigurationElement
<ConfigurationProperty("numLine", DefaultValue:="0", IsKey:=True)> _
<IntegerValidator(MinValue:=0, MaxValue:=200)> _
Public Property NumLinea() As Integer
Get
Return CType(Me("numLine"), Integer)
End Get
Set(ByVal value As Integer)
Me("numLine") = value
End Set
End Property
<ConfigurationProperty("zones")> _
Public ReadOnly Property CustomConfigElementCollection() As ZoomZonesCollection
Get
Return TryCast(Me("zones"), ZoomZonesCollection)
End Get
End Property
End Class
类 ZoomLineCollection
扩展了 ConfigurationElementCollection
类,并读取 zoomLine
元素的序列。 声明式配置 AddItemName:="zoomLine"
将 XML 节点与组成集合的相应类(在本例中为 zoomLine
)连接起来。
<ConfigurationCollection(GetType(ZoomLinea), AddItemName:="zoomLine")> _
Public Class ZoomLineCollection
Inherits ConfigurationElementCollection
Protected Overrides Function CreateNewElement() As ConfigurationElement
Return New ZoomLinea()
End Function
Public Sub Add(ByVal valueInteger As Integer)
Dim newElement As ZoomLinea = TryCast(Me.CreateNewElement(), ZoomLine)
newElement.NumLine = valueInteger
MyBase.BaseAdd(newElement)
End Sub
Protected Overrides Function GetElementKey_
(ByVal element As System.Configuration.ConfigurationElement) As Object
Dim elem As ZoomLine = TryCast(element, ZoomLine)
Return elem.NumLinea
End Function
End Class
剩余的两个类用于读取和收集 zoomZone
元素,遵循与之前相同的结构。
Public Class ZoomZone
Inherits ConfigurationElement
<ConfigurationProperty("numNode", DefaultValue:="-1", _
IsKey:=False, IsRequired:=False)> _
Public Property NumNode() As Integer
Get
Return CType(Me("numNode"), Integer)
End Get
Set(ByVal value As Integer)
Me("numNode") = value
End Set
End Property
<ConfigurationProperty("zoomValue", DefaultValue:="100", _
IsKey:=False, IsRequired:=False)> _
<IntegerValidator(MinValue:=100, MaxValue:=500)> _
Public Property ZoomValue() As Integer
Get
Return CType(Me("zoomValue"), Integer)
End Get
Set(ByVal value As Integer)
Me("zoomValue") = value
End Set
End Property
End Class
<ConfigurationCollection(GetType(ZoomZone), AddItemName:="zoomZone")> _
Public Class ZoomZonaCollection
Inherits ConfigurationElementCollection
Protected Overrides Function CreateNewElement() As ConfigurationElement
Return New ZoomZone()
End Function
Public Sub Add(ByVal valueString As String, ByVal valueInteger As Integer)
Dim newElement As ZoomZona = TryCast(Me.CreateNewElement(), ZoomZone)
newElement.NumNode = valueString
newElement.ZoomValue = valueInteger
MyBase.BaseAdd(newElement)
End Sub
Protected Overrides Function GetElementKey_
(ByVal element As System.Configuration.ConfigurationElement) As Object
Dim elem As ZoomZona = TryCast(element, ZoomZone)
Return elem.NumNode
End Function
End Class
结论
.NET 配置库提供了有用的功能,需要去发现,例如配置属性的声明式规范。
为了实现嵌套元素,我遵循了以下两个规则
- 对于每个元素序列,我实现了一个集合。
- 对于每个节点,我实现了一个配置元素类,并带有配置属性
- 用于每个属性
- 用于每个子节点
历史
- 2010年10月5日:初始发布