diff --git a/Modules.Config/ConfigAttributes.vb b/Modules.Config/ConfigAttributes.vb index 672423f2..6dafd891 100644 --- a/Modules.Config/ConfigAttributes.vb +++ b/Modules.Config/ConfigAttributes.vb @@ -1,9 +1,22 @@ Public Class ConfigAttributes + ''' + ''' The primary connection string. Will not be saved to userconfig. + ''' Public Class ConnectionStringAttribute Inherits Attribute End Class + ''' + ''' The test connection string. Will not be saved to userconfig. + ''' Public Class ConnectionStringTestAttribute Inherits Attribute End Class + + ''' + ''' Global setting. Will not be saved to userconfig. + ''' + Public Class GlobalSettingAttribute + Inherits Attribute + End Class End Class diff --git a/Modules.Config/ConfigManager.vb b/Modules.Config/ConfigManager.vb index 0017b6ae..0791a6f1 100644 --- a/Modules.Config/ConfigManager.vb +++ b/Modules.Config/ConfigManager.vb @@ -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 - - ''' - ''' The blueprint class from which the default config is created - ''' 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 + + ''' + ''' Returns the currently loaded config object + ''' + ''' Public ReadOnly Property Config As T ''' @@ -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 ''' @@ -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 ''' - ''' Config Class ''' Source config object ''' Target config object ''' List of Attribute type to exclude - 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) ''' ''' Filters a config object by copying all values except `ExcludedAttributeTypes` ''' - ''' Config Class ''' Config object ''' List of Attribute type to exclude ''' - 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 diff --git a/Modules.Config/ConfigSample.vb b/Modules.Config/ConfigSample.vb index 0f2f7e75..f2afaf11 100644 --- a/Modules.Config/ConfigSample.vb +++ b/Modules.Config/ConfigSample.vb @@ -7,4 +7,9 @@ Public Class ConfigSample Public Property ConnectionStringTest As String + + + Public Property GlobalSetting As Integer + + Public Property SomeSetting As Boolean End Class