add global setting attribute
This commit is contained in:
parent
528a341f7a
commit
ecdf35f6fe
@ -1,9 +1,22 @@
|
||||
Public Class ConfigAttributes
|
||||
''' <summary>
|
||||
''' The primary connection string. Will not be saved to userconfig.
|
||||
''' </summary>
|
||||
Public Class ConnectionStringAttribute
|
||||
Inherits Attribute
|
||||
End Class
|
||||
|
||||
''' <summary>
|
||||
''' The test connection string. Will not be saved to userconfig.
|
||||
''' </summary>
|
||||
Public Class ConnectionStringTestAttribute
|
||||
Inherits Attribute
|
||||
End Class
|
||||
|
||||
''' <summary>
|
||||
''' Global setting. Will not be saved to userconfig.
|
||||
''' </summary>
|
||||
Public Class GlobalSettingAttribute
|
||||
Inherits Attribute
|
||||
End Class
|
||||
End Class
|
||||
|
||||
@ -13,21 +13,24 @@ Public Class ConfigManager(Of T)
|
||||
Private ReadOnly _File As Filesystem.File
|
||||
Private ReadOnly _UserPath As String
|
||||
Private ReadOnly _ComputerPath As String
|
||||
Private ReadOnly _TestMode As Boolean = False
|
||||
|
||||
Private _UseUserConfig As Boolean = False
|
||||
Private _TestMode As Boolean = False
|
||||
|
||||
''' <summary>
|
||||
''' The blueprint class from which the default config is created
|
||||
''' </summary>
|
||||
Private ReadOnly _Blueprint As T
|
||||
Private ReadOnly _BlueprintType As Type
|
||||
Private ReadOnly _Serializer As XmlSerializer
|
||||
|
||||
Private ReadOnly _ExcludedAttributes = New List(Of Type) From {
|
||||
GetType(ConnectionStringAttribute),
|
||||
GetType(ConnectionStringTestAttribute)
|
||||
GetType(ConnectionStringTestAttribute),
|
||||
GetType(GlobalSettingAttribute)
|
||||
}
|
||||
|
||||
Private _ForceUserConfig As Boolean = False
|
||||
|
||||
''' <summary>
|
||||
''' Returns the currently loaded config object
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public ReadOnly Property Config As T
|
||||
|
||||
''' <summary>
|
||||
@ -45,10 +48,11 @@ Public Class ConfigManager(Of T)
|
||||
|
||||
_UserPath = Path.Combine(UserConfigPath, USER_CONFIG_NAME)
|
||||
_ComputerPath = Path.Combine(ComputerConfigPath, COMPUTER_CONFIG_NAME)
|
||||
_UseUserConfig = ForceUserConfig
|
||||
_ForceUserConfig = ForceUserConfig
|
||||
|
||||
_Blueprint = Activator.CreateInstance(Of T)
|
||||
_Serializer = New XmlSerializer(_Blueprint.GetType)
|
||||
_BlueprintType = _Blueprint.GetType
|
||||
_Serializer = New XmlSerializer(_BlueprintType)
|
||||
|
||||
If Not Directory.Exists(UserConfigPath) Then
|
||||
_Logger.Debug("UserConfigPath {0} did not exist and was created", UserConfigPath)
|
||||
@ -71,8 +75,6 @@ Public Class ConfigManager(Of T)
|
||||
End If
|
||||
|
||||
_Config = LoadConfig()
|
||||
|
||||
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
@ -103,11 +105,10 @@ Public Class ConfigManager(Of T)
|
||||
''' Copies all properties from Source to Target, except those who have an attribute
|
||||
''' listed in ExcludedAttributeTypes
|
||||
''' </summary>
|
||||
''' <typeparam name="T">Config Class</typeparam>
|
||||
''' <param name="Source">Source config object</param>
|
||||
''' <param name="Target">Target config object</param>
|
||||
''' <param name="ExcludedAttributeTypes">List of Attribute type to exclude</param>
|
||||
Private Sub CopyValues(Of T)(Source As T, Target As T, Optional ExcludedAttributeTypes As List(Of Type) = Nothing)
|
||||
Private Sub CopyValues(Source As T, Target As T, Optional ExcludedAttributeTypes As List(Of Type) = Nothing)
|
||||
Dim oType As Type = GetType(T)
|
||||
Dim oExcludedAttributeTypes = IIf(IsNothing(ExcludedAttributeTypes), New List(Of Type), ExcludedAttributeTypes)
|
||||
Dim oProperties = oType.GetProperties().
|
||||
@ -132,11 +133,10 @@ Public Class ConfigManager(Of T)
|
||||
''' <summary>
|
||||
''' Filters a config object by copying all values except `ExcludedAttributeTypes`
|
||||
''' </summary>
|
||||
''' <typeparam name="T">Config Class</typeparam>
|
||||
''' <param name="Data">Config object</param>
|
||||
''' <param name="ExcludedAttributeTypes">List of Attribute type to exclude</param>
|
||||
''' <returns></returns>
|
||||
Private Function FilterValues(Of T)(ByVal Data As T, ExcludedAttributeTypes As List(Of Type)) As T
|
||||
Private Function FilterValues(ByVal Data As T, ExcludedAttributeTypes As List(Of Type)) As T
|
||||
Dim oResult As T = Activator.CreateInstance(Of T)
|
||||
|
||||
CopyValues(Data, oResult, ExcludedAttributeTypes)
|
||||
@ -145,7 +145,7 @@ Public Class ConfigManager(Of T)
|
||||
|
||||
Private Function LoadConfig() As T
|
||||
' first create an empty/default config object
|
||||
Dim oConfig = Activator.CreateInstance(_Blueprint.GetType)
|
||||
Dim oConfig = Activator.CreateInstance(_BlueprintType)
|
||||
' then Try to load computer config
|
||||
oConfig = LoadComputerConfig(oConfig)
|
||||
' now try to load userconfig
|
||||
@ -153,6 +153,7 @@ Public Class ConfigManager(Of T)
|
||||
Return oConfig
|
||||
End Function
|
||||
|
||||
|
||||
Private Function LoadComputerConfig(ByVal Config As T) As T
|
||||
If File.Exists(_ComputerPath) Then
|
||||
Try
|
||||
@ -163,7 +164,7 @@ Public Class ConfigManager(Of T)
|
||||
_Logger.Warn("Computer config could not be loaded!")
|
||||
End Try
|
||||
Else
|
||||
_UseUserConfig = True
|
||||
_ForceUserConfig = True
|
||||
End If
|
||||
|
||||
Return Config
|
||||
@ -179,14 +180,12 @@ Public Class ConfigManager(Of T)
|
||||
Dim oExcludedAttributes As New List(Of Type)
|
||||
|
||||
' Copy values from user config to final config
|
||||
If _UseUserConfig Then
|
||||
If _ForceUserConfig Then
|
||||
CopyValues(oUserConfig, Config, New List(Of Type))
|
||||
Else
|
||||
CopyValues(oUserConfig, Config, _ExcludedAttributes)
|
||||
End If
|
||||
End If
|
||||
|
||||
'Dim oConnectionProperty = TestHasAttribute(oConfig, GetType(ConnectionStringAttribute))
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
_Logger.Warn("User config could not be loaded!")
|
||||
@ -236,7 +235,7 @@ Public Class ConfigManager(Of T)
|
||||
|
||||
' If config was loaded from computer config,
|
||||
' DO NOT save connection string, etc. to user config
|
||||
If _UseUserConfig = False And ForceAll = False Then
|
||||
If _ForceUserConfig = False And ForceAll = False Then
|
||||
Data = FilterValues(Data, _ExcludedAttributes)
|
||||
End If
|
||||
|
||||
@ -269,7 +268,7 @@ Public Class ConfigManager(Of T)
|
||||
' In this case we need to create oConfig from defaults so we have at least some config object
|
||||
If oConfig Is Nothing Then
|
||||
_Logger.Debug("Config file is valid but empty. Loading default values")
|
||||
oConfig = Activator.CreateInstance(_Blueprint.GetType)
|
||||
oConfig = Activator.CreateInstance(_BlueprintType)
|
||||
End If
|
||||
|
||||
Return oConfig
|
||||
|
||||
@ -7,4 +7,9 @@ Public Class ConfigSample
|
||||
|
||||
<ConnectionStringTest>
|
||||
Public Property ConnectionStringTest As String
|
||||
|
||||
<GlobalSetting>
|
||||
Public Property GlobalSetting As Integer
|
||||
|
||||
Public Property SomeSetting As Boolean
|
||||
End Class
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user