add global setting attribute

This commit is contained in:
Jonathan Jenne 2019-04-29 10:39:45 +02:00
parent 528a341f7a
commit ecdf35f6fe
3 changed files with 39 additions and 22 deletions

View File

@ -1,9 +1,22 @@
Public Class ConfigAttributes Public Class ConfigAttributes
''' <summary>
''' The primary connection string. Will not be saved to userconfig.
''' </summary>
Public Class ConnectionStringAttribute Public Class ConnectionStringAttribute
Inherits Attribute Inherits Attribute
End Class End Class
''' <summary>
''' The test connection string. Will not be saved to userconfig.
''' </summary>
Public Class ConnectionStringTestAttribute Public Class ConnectionStringTestAttribute
Inherits Attribute Inherits Attribute
End Class End Class
''' <summary>
''' Global setting. Will not be saved to userconfig.
''' </summary>
Public Class GlobalSettingAttribute
Inherits Attribute
End Class
End Class End Class

View File

@ -13,21 +13,24 @@ Public Class ConfigManager(Of T)
Private ReadOnly _File As Filesystem.File Private ReadOnly _File As Filesystem.File
Private ReadOnly _UserPath As String Private ReadOnly _UserPath As String
Private ReadOnly _ComputerPath 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 _Blueprint As T
Private ReadOnly _BlueprintType As Type
Private ReadOnly _Serializer As XmlSerializer Private ReadOnly _Serializer As XmlSerializer
Private ReadOnly _ExcludedAttributes = New List(Of Type) From { Private ReadOnly _ExcludedAttributes = New List(Of Type) From {
GetType(ConnectionStringAttribute), 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 Public ReadOnly Property Config As T
''' <summary> ''' <summary>
@ -45,10 +48,11 @@ Public Class ConfigManager(Of T)
_UserPath = Path.Combine(UserConfigPath, USER_CONFIG_NAME) _UserPath = Path.Combine(UserConfigPath, USER_CONFIG_NAME)
_ComputerPath = Path.Combine(ComputerConfigPath, COMPUTER_CONFIG_NAME) _ComputerPath = Path.Combine(ComputerConfigPath, COMPUTER_CONFIG_NAME)
_UseUserConfig = ForceUserConfig _ForceUserConfig = ForceUserConfig
_Blueprint = Activator.CreateInstance(Of T) _Blueprint = Activator.CreateInstance(Of T)
_Serializer = New XmlSerializer(_Blueprint.GetType) _BlueprintType = _Blueprint.GetType
_Serializer = New XmlSerializer(_BlueprintType)
If Not Directory.Exists(UserConfigPath) Then If Not Directory.Exists(UserConfigPath) Then
_Logger.Debug("UserConfigPath {0} did not exist and was created", UserConfigPath) _Logger.Debug("UserConfigPath {0} did not exist and was created", UserConfigPath)
@ -71,8 +75,6 @@ Public Class ConfigManager(Of T)
End If End If
_Config = LoadConfig() _Config = LoadConfig()
End Sub End Sub
''' <summary> ''' <summary>
@ -103,11 +105,10 @@ Public Class ConfigManager(Of T)
''' Copies all properties from Source to Target, except those who have an attribute ''' Copies all properties from Source to Target, except those who have an attribute
''' listed in ExcludedAttributeTypes ''' listed in ExcludedAttributeTypes
''' </summary> ''' </summary>
''' <typeparam name="T">Config Class</typeparam>
''' <param name="Source">Source config object</param> ''' <param name="Source">Source config object</param>
''' <param name="Target">Target config object</param> ''' <param name="Target">Target config object</param>
''' <param name="ExcludedAttributeTypes">List of Attribute type to exclude</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 oType As Type = GetType(T)
Dim oExcludedAttributeTypes = IIf(IsNothing(ExcludedAttributeTypes), New List(Of Type), ExcludedAttributeTypes) Dim oExcludedAttributeTypes = IIf(IsNothing(ExcludedAttributeTypes), New List(Of Type), ExcludedAttributeTypes)
Dim oProperties = oType.GetProperties(). Dim oProperties = oType.GetProperties().
@ -132,11 +133,10 @@ Public Class ConfigManager(Of T)
''' <summary> ''' <summary>
''' Filters a config object by copying all values except `ExcludedAttributeTypes` ''' Filters a config object by copying all values except `ExcludedAttributeTypes`
''' </summary> ''' </summary>
''' <typeparam name="T">Config Class</typeparam>
''' <param name="Data">Config object</param> ''' <param name="Data">Config object</param>
''' <param name="ExcludedAttributeTypes">List of Attribute type to exclude</param> ''' <param name="ExcludedAttributeTypes">List of Attribute type to exclude</param>
''' <returns></returns> ''' <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) Dim oResult As T = Activator.CreateInstance(Of T)
CopyValues(Data, oResult, ExcludedAttributeTypes) CopyValues(Data, oResult, ExcludedAttributeTypes)
@ -145,7 +145,7 @@ Public Class ConfigManager(Of T)
Private Function LoadConfig() As T Private Function LoadConfig() As T
' first create an empty/default config object ' first create an empty/default config object
Dim oConfig = Activator.CreateInstance(_Blueprint.GetType) Dim oConfig = Activator.CreateInstance(_BlueprintType)
' then Try to load computer config ' then Try to load computer config
oConfig = LoadComputerConfig(oConfig) oConfig = LoadComputerConfig(oConfig)
' now try to load userconfig ' now try to load userconfig
@ -153,6 +153,7 @@ Public Class ConfigManager(Of T)
Return oConfig Return oConfig
End Function End Function
Private Function LoadComputerConfig(ByVal Config As T) As T Private Function LoadComputerConfig(ByVal Config As T) As T
If File.Exists(_ComputerPath) Then If File.Exists(_ComputerPath) Then
Try Try
@ -163,7 +164,7 @@ Public Class ConfigManager(Of T)
_Logger.Warn("Computer config could not be loaded!") _Logger.Warn("Computer config could not be loaded!")
End Try End Try
Else Else
_UseUserConfig = True _ForceUserConfig = True
End If End If
Return Config Return Config
@ -179,14 +180,12 @@ Public Class ConfigManager(Of T)
Dim oExcludedAttributes As New List(Of Type) Dim oExcludedAttributes As New List(Of Type)
' Copy values from user config to final config ' Copy values from user config to final config
If _UseUserConfig Then If _ForceUserConfig Then
CopyValues(oUserConfig, Config, New List(Of Type)) CopyValues(oUserConfig, Config, New List(Of Type))
Else Else
CopyValues(oUserConfig, Config, _ExcludedAttributes) CopyValues(oUserConfig, Config, _ExcludedAttributes)
End If End If
End If End If
'Dim oConnectionProperty = TestHasAttribute(oConfig, GetType(ConnectionStringAttribute))
Catch ex As Exception Catch ex As Exception
_Logger.Error(ex) _Logger.Error(ex)
_Logger.Warn("User config could not be loaded!") _Logger.Warn("User config could not be loaded!")
@ -236,7 +235,7 @@ Public Class ConfigManager(Of T)
' If config was loaded from computer config, ' If config was loaded from computer config,
' DO NOT save connection string, etc. to user 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) Data = FilterValues(Data, _ExcludedAttributes)
End If 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 ' In this case we need to create oConfig from defaults so we have at least some config object
If oConfig Is Nothing Then If oConfig Is Nothing Then
_Logger.Debug("Config file is valid but empty. Loading default values") _Logger.Debug("Config file is valid but empty. Loading default values")
oConfig = Activator.CreateInstance(_Blueprint.GetType) oConfig = Activator.CreateInstance(_BlueprintType)
End If End If
Return oConfig Return oConfig

View File

@ -7,4 +7,9 @@ Public Class ConfigSample
<ConnectionStringTest> <ConnectionStringTest>
Public Property ConnectionStringTest As String Public Property ConnectionStringTest As String
<GlobalSetting>
Public Property GlobalSetting As Integer
Public Property SomeSetting As Boolean
End Class End Class