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 _UserPath As String
|
||||||
Private ReadOnly _ComputerPath As String
|
Private ReadOnly _ComputerPath As String
|
||||||
|
|
||||||
Private _ForceUserConfig As Boolean = False
|
Private _UseUserConfig As Boolean = False
|
||||||
Private _TestMode As Boolean = False
|
Private _TestMode As Boolean = False
|
||||||
|
|
||||||
''' <summary>
|
''' <summary>
|
||||||
@ -23,6 +23,11 @@ Public Class ConfigManager(Of T)
|
|||||||
Private ReadOnly _Blueprint As T
|
Private ReadOnly _Blueprint As T
|
||||||
Private ReadOnly _Serializer As XmlSerializer
|
Private ReadOnly _Serializer As XmlSerializer
|
||||||
|
|
||||||
|
Private ReadOnly _ExcludedAttributes = New List(Of Type) From {
|
||||||
|
GetType(ConnectionStringAttribute),
|
||||||
|
GetType(ConnectionStringTestAttribute)
|
||||||
|
}
|
||||||
|
|
||||||
Public ReadOnly Property Config As T
|
Public ReadOnly Property Config As T
|
||||||
|
|
||||||
''' <summary>
|
''' <summary>
|
||||||
@ -40,7 +45,7 @@ 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)
|
||||||
_ForceUserConfig = ForceUserConfig
|
_UseUserConfig = ForceUserConfig
|
||||||
|
|
||||||
_Blueprint = Activator.CreateInstance(Of T)
|
_Blueprint = Activator.CreateInstance(Of T)
|
||||||
_Serializer = New XmlSerializer(_Blueprint.GetType)
|
_Serializer = New XmlSerializer(_Blueprint.GetType)
|
||||||
@ -66,6 +71,8 @@ Public Class ConfigManager(Of T)
|
|||||||
End If
|
End If
|
||||||
|
|
||||||
_Config = LoadConfig()
|
_Config = LoadConfig()
|
||||||
|
|
||||||
|
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
''' <summary>
|
''' <summary>
|
||||||
@ -80,10 +87,11 @@ Public Class ConfigManager(Of T)
|
|||||||
''' <summary>
|
''' <summary>
|
||||||
''' Save the current config object to `UserConfigPath`
|
''' Save the current config object to `UserConfigPath`
|
||||||
''' </summary>
|
''' </summary>
|
||||||
|
''' <param name="ForceAll">Force saving all attributes including the attributes marked as excluded</param>
|
||||||
''' <returns>True if save was successful, False otherwise</returns>
|
''' <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
|
Try
|
||||||
WriteToFile(_Config, _UserPath)
|
WriteToFile(_Config, _UserPath, ForceAll)
|
||||||
Return True
|
Return True
|
||||||
Catch ex As Exception
|
Catch ex As Exception
|
||||||
_Logger.Error(ex)
|
_Logger.Error(ex)
|
||||||
@ -121,6 +129,20 @@ Public Class ConfigManager(Of T)
|
|||||||
Next
|
Next
|
||||||
End Sub
|
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
|
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(_Blueprint.GetType)
|
||||||
@ -141,7 +163,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
|
||||||
_ForceUserConfig = True
|
_UseUserConfig = True
|
||||||
End If
|
End If
|
||||||
|
|
||||||
Return Config
|
Return Config
|
||||||
@ -157,13 +179,10 @@ 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 _ForceUserConfig Then
|
If _UseUserConfig Then
|
||||||
CopyValues(oUserConfig, Config, New List(Of Type))
|
CopyValues(oUserConfig, Config, New List(Of Type))
|
||||||
Else
|
Else
|
||||||
CopyValues(oUserConfig, Config, New List(Of Type) From {
|
CopyValues(oUserConfig, Config, _ExcludedAttributes)
|
||||||
GetType(ConnectionStringAttribute),
|
|
||||||
GetType(ConnectionStringTestAttribute)
|
|
||||||
})
|
|
||||||
End If
|
End If
|
||||||
End If
|
End If
|
||||||
|
|
||||||
@ -177,19 +196,6 @@ Public Class ConfigManager(Of T)
|
|||||||
Return Config
|
Return Config
|
||||||
End Function
|
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
|
Private Function TestHasAttribute(Config As T, AttributeType As Type) As Boolean
|
||||||
For Each oProperty As PropertyInfo In Config.GetType.GetProperties()
|
For Each oProperty As PropertyInfo In Config.GetType.GetProperties()
|
||||||
If Attribute.IsDefined(oProperty, GetType(ConnectionStringAttribute)) Then
|
If Attribute.IsDefined(oProperty, GetType(ConnectionStringAttribute)) Then
|
||||||
@ -224,9 +230,16 @@ Public Class ConfigManager(Of T)
|
|||||||
''' </summary>
|
''' </summary>
|
||||||
''' <param name="Data">The object to write</param>
|
''' <param name="Data">The object to write</param>
|
||||||
''' <param name="Path">The file name to write to</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
|
Try
|
||||||
_Logger.Debug("Saving config to: {0}", Path)
|
_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)
|
Dim oBytes = Serialize(Data)
|
||||||
|
|
||||||
Using oFileStream = New FileStream(Path, FileMode.Create, FileAccess.Write)
|
Using oFileStream = New FileStream(Path, FileMode.Create, FileAccess.Write)
|
||||||
|
|||||||
@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
|
|||||||
' übernehmen, indem Sie "*" eingeben:
|
' übernehmen, indem Sie "*" eingeben:
|
||||||
' <Assembly: AssemblyVersion("1.0.*")>
|
' <Assembly: AssemblyVersion("1.0.*")>
|
||||||
|
|
||||||
<Assembly: AssemblyVersion("0.0.3.0")>
|
<Assembly: AssemblyVersion("0.0.5.0")>
|
||||||
<Assembly: AssemblyFileVersion("1.0.0.0")>
|
<Assembly: AssemblyFileVersion("1.0.0.0")>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user