Add Force Option to Save
This commit is contained in:
parent
68dc46c6b4
commit
c8c6f3ab3d
@ -14,7 +14,7 @@ Public Class ConfigManager(Of T)
|
||||
Private ReadOnly _UserPath As String
|
||||
Private ReadOnly _ComputerPath As String
|
||||
|
||||
Private _ForceUserConfig As Boolean = False
|
||||
Private _UseUserConfig As Boolean = False
|
||||
Private _TestMode As Boolean = False
|
||||
|
||||
''' <summary>
|
||||
@ -23,6 +23,11 @@ Public Class ConfigManager(Of T)
|
||||
Private ReadOnly _Blueprint As T
|
||||
Private ReadOnly _Serializer As XmlSerializer
|
||||
|
||||
Private ReadOnly _ExcludedAttributes = New List(Of Type) From {
|
||||
GetType(ConnectionStringAttribute),
|
||||
GetType(ConnectionStringTestAttribute)
|
||||
}
|
||||
|
||||
Public ReadOnly Property Config As T
|
||||
|
||||
''' <summary>
|
||||
@ -40,7 +45,7 @@ Public Class ConfigManager(Of T)
|
||||
|
||||
_UserPath = Path.Combine(UserConfigPath, USER_CONFIG_NAME)
|
||||
_ComputerPath = Path.Combine(ComputerConfigPath, COMPUTER_CONFIG_NAME)
|
||||
_ForceUserConfig = ForceUserConfig
|
||||
_UseUserConfig = ForceUserConfig
|
||||
|
||||
_Blueprint = Activator.CreateInstance(Of T)
|
||||
_Serializer = New XmlSerializer(_Blueprint.GetType)
|
||||
@ -66,6 +71,8 @@ Public Class ConfigManager(Of T)
|
||||
End If
|
||||
|
||||
_Config = LoadConfig()
|
||||
|
||||
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
@ -80,10 +87,11 @@ Public Class ConfigManager(Of T)
|
||||
''' <summary>
|
||||
''' Save the current config object to `UserConfigPath`
|
||||
''' </summary>
|
||||
''' <param name="ForceAll">Force saving all attributes including the attributes marked as excluded</param>
|
||||
''' <returns>True if save was successful, False otherwise</returns>
|
||||
Public Function Save() As Boolean
|
||||
Public Function Save(Optional ForceAll As Boolean = False) As Boolean
|
||||
Try
|
||||
WriteToFile(_Config, _UserPath)
|
||||
WriteToFile(_Config, _UserPath, ForceAll)
|
||||
Return True
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
@ -121,6 +129,20 @@ Public Class ConfigManager(Of T)
|
||||
Next
|
||||
End Sub
|
||||
|
||||
''' <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
|
||||
Dim oResult As T = Activator.CreateInstance(Of T)
|
||||
|
||||
CopyValues(Data, oResult, ExcludedAttributeTypes)
|
||||
Return oResult
|
||||
End Function
|
||||
|
||||
Private Function LoadConfig() As T
|
||||
' first create an empty/default config object
|
||||
Dim oConfig = Activator.CreateInstance(_Blueprint.GetType)
|
||||
@ -141,7 +163,7 @@ Public Class ConfigManager(Of T)
|
||||
_Logger.Warn("Computer config could not be loaded!")
|
||||
End Try
|
||||
Else
|
||||
_ForceUserConfig = True
|
||||
_UseUserConfig = True
|
||||
End If
|
||||
|
||||
Return Config
|
||||
@ -157,13 +179,10 @@ Public Class ConfigManager(Of T)
|
||||
Dim oExcludedAttributes As New List(Of Type)
|
||||
|
||||
' Copy values from user config to final config
|
||||
If _ForceUserConfig Then
|
||||
If _UseUserConfig Then
|
||||
CopyValues(oUserConfig, Config, New List(Of Type))
|
||||
Else
|
||||
CopyValues(oUserConfig, Config, New List(Of Type) From {
|
||||
GetType(ConnectionStringAttribute),
|
||||
GetType(ConnectionStringTestAttribute)
|
||||
})
|
||||
CopyValues(oUserConfig, Config, _ExcludedAttributes)
|
||||
End If
|
||||
End If
|
||||
|
||||
@ -177,19 +196,6 @@ Public Class ConfigManager(Of T)
|
||||
Return Config
|
||||
End Function
|
||||
|
||||
Private Function LoadDefaultConfig() As T
|
||||
_Logger.Debug("Creating default config in UserPath: {0}", _UserPath)
|
||||
Dim oConfig = Activator.CreateInstance(_Blueprint.GetType)
|
||||
|
||||
Try
|
||||
WriteToFile(oConfig, _UserPath)
|
||||
Catch ex As Exception
|
||||
_Logger.Warn("Could not create default config in UserPath: {0}", _UserPath)
|
||||
End Try
|
||||
|
||||
Return oConfig
|
||||
End Function
|
||||
|
||||
Private Function TestHasAttribute(Config As T, AttributeType As Type) As Boolean
|
||||
For Each oProperty As PropertyInfo In Config.GetType.GetProperties()
|
||||
If Attribute.IsDefined(oProperty, GetType(ConnectionStringAttribute)) Then
|
||||
@ -224,9 +230,16 @@ Public Class ConfigManager(Of T)
|
||||
''' </summary>
|
||||
''' <param name="Data">The object to write</param>
|
||||
''' <param name="Path">The file name to write to</param>
|
||||
Private Sub WriteToFile(Data As T, Path As String)
|
||||
Private Sub WriteToFile(Data As T, Path As String, ForceAll As Boolean)
|
||||
Try
|
||||
_Logger.Debug("Saving config to: {0}", Path)
|
||||
|
||||
' If config was loaded from computer config,
|
||||
' DO NOT save connection string, etc. to user config
|
||||
If _UseUserConfig = False And ForceAll = False Then
|
||||
Data = FilterValues(Data, _ExcludedAttributes)
|
||||
End If
|
||||
|
||||
Dim oBytes = Serialize(Data)
|
||||
|
||||
Using oFileStream = New FileStream(Path, FileMode.Create, FileAccess.Write)
|
||||
|
||||
@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
|
||||
' übernehmen, indem Sie "*" eingeben:
|
||||
' <Assembly: AssemblyVersion("1.0.*")>
|
||||
|
||||
<Assembly: AssemblyVersion("0.0.3.0")>
|
||||
<Assembly: AssemblyVersion("0.0.5.0")>
|
||||
<Assembly: AssemblyFileVersion("1.0.0.0")>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user