Config Manager WIP
This commit is contained in:
@@ -54,6 +54,7 @@
|
||||
<Reference Include="System.Runtime.Serialization" />
|
||||
<Reference Include="System.ServiceModel" />
|
||||
<Reference Include="System.Transactions" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
@@ -72,7 +73,8 @@
|
||||
<Import Include="System.Threading.Tasks" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="BaseConfig.vb" />
|
||||
<Compile Include="OldConfig.vb" />
|
||||
<Compile Include="ConfigManager.vb" />
|
||||
<Compile Include="My Project\AssemblyInfo.vb" />
|
||||
<Compile Include="My Project\Application.Designer.vb">
|
||||
<AutoGen>True</AutoGen>
|
||||
@@ -88,6 +90,7 @@
|
||||
<DependentUpon>Settings.settings</DependentUpon>
|
||||
<DesignTimeSharedInput>True</DesignTimeSharedInput>
|
||||
</Compile>
|
||||
<Compile Include="SinceVersionAttribute.vb" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="My Project\Resources.resx">
|
||||
@@ -110,6 +113,10 @@
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Filesystem\Filesystem.vbproj">
|
||||
<Project>{991d0231-4623-496d-8bd0-9ca906029cbc}</Project>
|
||||
<Name>Filesystem</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Modules.Logging\Logging.vbproj">
|
||||
<Project>{903b2d7d-3b80-4be9-8713-7447b704e1b0}</Project>
|
||||
<Name>Logging</Name>
|
||||
|
||||
142
Config/ConfigManager.vb
Normal file
142
Config/ConfigManager.vb
Normal file
@@ -0,0 +1,142 @@
|
||||
Imports System.IO
|
||||
Imports System.Xml.Serialization
|
||||
Imports DigitalData.Modules.Logging
|
||||
Imports DigitalData.Modules.Filesystem
|
||||
|
||||
Public Class ConfigManager(Of T)
|
||||
Private Const USER_CONFIG_NAME As String = "UserConfig.xml"
|
||||
Private Const COMPUTER_CONFIG_NAME As String = "ComputerConfig.xml"
|
||||
|
||||
Private ReadOnly _LogConfig As LogConfig
|
||||
Private ReadOnly _Logger As Logger
|
||||
Private ReadOnly _File As Filesystem.File
|
||||
Private ReadOnly _UserPath As String
|
||||
Private ReadOnly _ComputerPath As String
|
||||
Private ReadOnly _CurrentDataPath As String
|
||||
|
||||
Private ReadOnly _Schema As T
|
||||
Private ReadOnly _Serializer As XmlSerializer
|
||||
|
||||
Public ReadOnly Property Config As T
|
||||
|
||||
''' <summary>
|
||||
''' Creates a new instance of the ConfigManager
|
||||
''' </summary>
|
||||
''' <example>
|
||||
''' Public Class Config
|
||||
''' Public Property StringEntry As String = "TEST"
|
||||
''' Public Property BoolEntry As Boolean = True
|
||||
''' Public Property IntEntry As Integer = 123
|
||||
''' End Class
|
||||
'''
|
||||
''' Dim oConfigManager = New ConfigManager(Of Config)(_LogConfig, Application.UserAppDataPath, Application.CommonAppDataPath)
|
||||
''' </example>
|
||||
''' <param name="LogConfig">LogConfig instance</param>
|
||||
''' <param name="UserConfigPath">The first path to check for a config file, eg. AppData</param>
|
||||
''' <param name="ComputerConfigPath">The second path to check for a config file, eg. ProgramData</param>
|
||||
Public Sub New(LogConfig As LogConfig, UserConfigPath As String, ComputerConfigPath As String)
|
||||
_LogConfig = LogConfig
|
||||
_Logger = LogConfig.GetLogger()
|
||||
_File = New Filesystem.File(_LogConfig)
|
||||
|
||||
_UserPath = Path.Combine(UserConfigPath, USER_CONFIG_NAME)
|
||||
_ComputerPath = Path.Combine(ComputerConfigPath, COMPUTER_CONFIG_NAME)
|
||||
|
||||
_Schema = Activator.CreateInstance(Of T)
|
||||
_Serializer = New XmlSerializer(_Schema.GetType)
|
||||
|
||||
If Not Directory.Exists(UserConfigPath) Then
|
||||
Throw New DirectoryNotFoundException($"Path {UserConfigPath} does not exist!")
|
||||
End If
|
||||
|
||||
If Not Directory.Exists(ComputerConfigPath) Then
|
||||
Throw New DirectoryNotFoundException($"Path {ComputerConfigPath} does not exist!")
|
||||
End If
|
||||
|
||||
If Not Filesystem.File Then Then
|
||||
Throw New DirectoryNotFoundException()
|
||||
End If
|
||||
|
||||
If IO.File.Exists(_UserPath) Then
|
||||
_Logger.Debug("Loading config from UserPath: {0}", _UserPath)
|
||||
_CurrentDataPath = _UserPath
|
||||
_Config = ReadFromFile(_UserPath)
|
||||
ElseIf IO.File.Exists(_ComputerPath) Then
|
||||
_Logger.Debug("Loading config from ComputerPath: {0}", _ComputerPath)
|
||||
_CurrentDataPath = _ComputerPath
|
||||
_Config = ReadFromFile(_ComputerPath)
|
||||
Else
|
||||
_Logger.Debug("Creating default config in UserPath: {0}", _UserPath)
|
||||
_CurrentDataPath = _UserPath
|
||||
_Config = Activator.CreateInstance(_Schema.GetType)
|
||||
|
||||
WriteToFile(_Config, _UserPath)
|
||||
End If
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' Save the current config object to `UserConfigPath`
|
||||
''' </summary>
|
||||
Public Sub Save()
|
||||
WriteToFile(_Config, _UserPath)
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' Serialize a config object to byte array
|
||||
''' </summary>
|
||||
''' <param name="Data"></param>
|
||||
''' <returns></returns>
|
||||
Private Function Serialize(Data As T) As Byte()
|
||||
Try
|
||||
_Logger.Debug("Serializing config object")
|
||||
|
||||
Using oStream = New MemoryStream()
|
||||
_Serializer.Serialize(oStream, Data)
|
||||
Return oStream.ToArray()
|
||||
End Using
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Write an object to disk as xml
|
||||
''' </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)
|
||||
Try
|
||||
_Logger.Debug("Saving config to: {0}", Path)
|
||||
Dim oBytes = Serialize(Data)
|
||||
|
||||
Using oFileStream = New FileStream(Path, FileMode.Create, FileAccess.Write)
|
||||
oFileStream.Write(oBytes, 0, oBytes.Length)
|
||||
oFileStream.Flush()
|
||||
End Using
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' Reads an xml from disk and deserializes to object
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Private Function ReadFromFile(Path As String) As T
|
||||
Try
|
||||
_Logger.Debug("Loading config from: {0}", Path)
|
||||
Dim oConfig As T
|
||||
|
||||
Using oReader As New StreamReader(Path)
|
||||
oConfig = _Serializer.Deserialize(oReader)
|
||||
End Using
|
||||
|
||||
Return oConfig
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
End Class
|
||||
@@ -1,7 +1,7 @@
|
||||
Imports System.IO
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Public MustInherit Class BaseConfig
|
||||
Public MustInherit Class OldConfig
|
||||
Private Const _userConfigFileName As String = "UserConfig.xml"
|
||||
Private _logFactory As LogConfig
|
||||
Private _logger As Logger
|
||||
9
Config/SinceVersionAttribute.vb
Normal file
9
Config/SinceVersionAttribute.vb
Normal file
@@ -0,0 +1,9 @@
|
||||
Public Class SinceVersionAttribute
|
||||
Inherits Attribute
|
||||
|
||||
Public Version As Version
|
||||
|
||||
Public Sub New(Version As String)
|
||||
Me.Version = New Version(Version)
|
||||
End Sub
|
||||
End Class
|
||||
Reference in New Issue
Block a user