config in client suite

This commit is contained in:
Jonathan Jenne 2019-02-13 16:23:02 +01:00
parent 9ee59ac400
commit 914ba9dc90
11 changed files with 64 additions and 103 deletions

View File

@ -73,7 +73,6 @@
<Import Include="System.Threading.Tasks" /> <Import Include="System.Threading.Tasks" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="OldConfig.vb" />
<Compile Include="ConfigManager.vb" /> <Compile Include="ConfigManager.vb" />
<Compile Include="My Project\AssemblyInfo.vb" /> <Compile Include="My Project\AssemblyInfo.vb" />
<Compile Include="My Project\Application.Designer.vb"> <Compile Include="My Project\Application.Designer.vb">

View File

@ -12,7 +12,7 @@ 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 _CurrentDataPath As String Private _CurrentDataPath As String
Private ReadOnly _Schema As T Private ReadOnly _Schema As T
Private ReadOnly _Serializer As XmlSerializer Private ReadOnly _Serializer As XmlSerializer
@ -53,25 +53,15 @@ Public Class ConfigManager(Of T)
Throw New DirectoryNotFoundException($"Path {ComputerConfigPath} does not exist!") Throw New DirectoryNotFoundException($"Path {ComputerConfigPath} does not exist!")
End If End If
If Not Filesystem.File Then Then If Not _File.TestPathIsDirectory(UserConfigPath) Then
Throw New DirectoryNotFoundException() Throw New ArgumentException($"Path {UserConfigPath} is not a directory!")
End If End If
If IO.File.Exists(_UserPath) Then If Not _File.TestPathIsDirectory(ComputerConfigPath) Then
_Logger.Debug("Loading config from UserPath: {0}", _UserPath) Throw New ArgumentException($"Path {ComputerConfigPath} is not a directory!")
_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 If
_Config = LoadConfig()
End Sub End Sub
''' <summary> ''' <summary>
@ -81,6 +71,34 @@ Public Class ConfigManager(Of T)
WriteToFile(_Config, _UserPath) WriteToFile(_Config, _UserPath)
End Sub End Sub
''' <summary>
''' First check if a user config exists and if it does, load it.
''' If not, check if a systemwide config exists and and if it does, load it.
''' Otherwise, create a user config using the default values from the supplied config class `T`
''' </summary>
''' <returns></returns>
Private Function LoadConfig() As T
Dim oConfig As T
If IO.File.Exists(_UserPath) Then
_Logger.Debug("Loading config from UserPath: {0}", _UserPath)
_CurrentDataPath = _UserPath
oConfig = ReadFromFile(_UserPath)
ElseIf IO.File.Exists(_ComputerPath) Then
_Logger.Debug("Loading config from ComputerPath: {0}", _ComputerPath)
_CurrentDataPath = _ComputerPath
oConfig = ReadFromFile(_ComputerPath)
Else
_Logger.Debug("Creating default config in UserPath: {0}", _UserPath)
_CurrentDataPath = _UserPath
oConfig = Activator.CreateInstance(_Schema.GetType)
WriteToFile(_Config, _UserPath)
End If
Return oConfig
End Function
''' <summary> ''' <summary>
''' Serialize a config object to byte array ''' Serialize a config object to byte array
''' </summary> ''' </summary>

View File

@ -1,78 +0,0 @@
Imports System.IO
Imports DigitalData.Modules.Logging
Public MustInherit Class OldConfig
Private Const _userConfigFileName As String = "UserConfig.xml"
Private _logFactory As LogConfig
Private _logger As Logger
Protected Const _configKey As String = "Key"
Protected Const _configValue As String = "Value"
Protected _dataTable As DataTable
Protected ReadOnly Property PropertyNames As Dictionary(Of String, String)
Get
Return New Dictionary(Of String, String)
End Get
End Property
Public Sub New(LogConfig As LogConfig)
_logFactory = LogConfig
_logger = LogConfig.GetLogger()
End Sub
Private Function GetUserConfigPath() As String
Dim oAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Dim oCompanyName = My.Application.Info.CompanyName
Dim oProductName = My.Application.Info.ProductName
Return Path.Combine(oAppData, oCompanyName, oProductName, _userConfigFileName)
End Function
Protected Function LoadConfig() As DataTable
Dim oUserConfigPath As String = GetUserConfigPath()
Dim oDatatable As New DataTable()
If Not File.Exists(oUserConfigPath) Then
_logger.Warn("Config file {0} does not exist", oUserConfigPath)
Return Nothing
End If
Try
oDatatable.ReadXml(oUserConfigPath)
Return oDatatable
Catch ex As Exception
_logger.Error(ex)
Return Nothing
End Try
End Function
Protected Sub SaveConfig(DataTable As DataTable)
Dim oUserConfigPath As String = GetUserConfigPath()
Try
DataTable.WriteXml(oUserConfigPath)
Catch ex As Exception
_logger.Error(ex)
End Try
End Sub
Protected Function ConvertToDataTable(dictionary As Dictionary(Of String, String)) As DataTable
Dim oNewDataTable As New DataTable("Config")
oNewDataTable.Columns.Add(New DataColumn("Key", GetType(String)))
oNewDataTable.Columns.Add(New DataColumn("Value", GetType(String)))
For Each oProperty In dictionary
Dim oNewRow = oNewDataTable.NewRow()
oNewRow.Item("Key") = oProperty.Key
oNewRow.Item("Value") = oProperty.Value
oNewDataTable.Rows.Add(oNewRow)
Next
oNewDataTable.AcceptChanges()
Return oNewDataTable
End Function
End Class

View File

@ -1,4 +1,5 @@
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Config
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Logging.LogConfig Imports DigitalData.Modules.Logging.LogConfig
Namespace My Namespace My
@ -13,7 +14,10 @@ Namespace My
Public Sub App_Startup() Handles Me.Startup Public Sub App_Startup() Handles Me.Startup
Dim oLogConfig As New LogConfig(PathType.AppData) Dim oLogConfig As New LogConfig(PathType.AppData)
Dim oConfigManager As New ConfigManager(Of ClassConfig)(oLogConfig, Windows.Forms.Application.UserAppDataPath, Windows.Forms.Application.CommonAppDataPath)
LogConfig = oLogConfig LogConfig = oLogConfig
ConfigManager = oConfigManager
_Logger = LogConfig.GetLogger() _Logger = LogConfig.GetLogger()
_Logger.Info("Starting Client Suite..") _Logger.Info("Starting Client Suite..")

View File

@ -0,0 +1,3 @@
Public Class ClassConfig
Public Property ServiceConnection As String = String.Empty
End Class

View File

@ -1,5 +1,5 @@
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Logging.LogConfig Imports DigitalData.Modules.Config
Imports System.ServiceModel Imports System.ServiceModel
Imports EDMI_ClientSuite.NetworkService_DDEDM Imports EDMI_ClientSuite.NetworkService_DDEDM
Imports System.ServiceModel.Channels Imports System.ServiceModel.Channels
@ -19,8 +19,9 @@ Public Class ClassInit
_Logger = My.LogConfig.GetLogger() _Logger = My.LogConfig.GetLogger()
End Sub End Sub
Public Function TestConnectionURLExists() Public Function TestConnectionURLExists() As Boolean
Return My.Settings.ICMServiceAddress <> String.Empty Return My.ConfigManager.Config.ServiceConnection <> String.Empty
'Return My.Settings.ICMServiceAddress <> String.Empty
End Function End Function
Public Async Function TestConnectionAsync(EndpointURL As String) As Task(Of ConnectionTestResult) Public Async Function TestConnectionAsync(EndpointURL As String) As Task(Of ConnectionTestResult)
@ -44,7 +45,8 @@ Public Class ClassInit
End Function End Function
Public Function GetChannelFactory() As IChannelFactory(Of IEDMServiceChannel) Public Function GetChannelFactory() As IChannelFactory(Of IEDMServiceChannel)
Return GetChannelFactory(My.Settings.ICMServiceAddress) 'Return GetChannelFactory(My.Settings.ICMServiceAddress)
Return GetChannelFactory(My.ConfigManager.Config.ServiceConnection)
End Function End Function
Public Function GetChannelFactory(EndpointURL As String) As ChannelFactory(Of IEDMServiceChannel) Public Function GetChannelFactory(EndpointURL As String) As ChannelFactory(Of IEDMServiceChannel)

View File

@ -115,6 +115,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ApplicationEvents.vb" /> <Compile Include="ApplicationEvents.vb" />
<Compile Include="ClassConfig.vb" />
<Compile Include="ClassConstants.vb" /> <Compile Include="ClassConstants.vb" />
<Compile Include="ClassInit.vb" /> <Compile Include="ClassInit.vb" />
<Compile Include="ClassLayout.vb" /> <Compile Include="ClassLayout.vb" />
@ -368,6 +369,10 @@
<None Include="Resources\folder_go.png" /> <None Include="Resources\folder_go.png" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Config\Config.vbproj">
<Project>{44982F9B-6116-44E2-85D0-F39650B1EF99}</Project>
<Name>Config</Name>
</ProjectReference>
<ProjectReference Include="..\EDMI_FILE_OPs\EDMIFileOps.vbproj"> <ProjectReference Include="..\EDMI_FILE_OPs\EDMIFileOps.vbproj">
<Project>{5b1171dc-fffe-4813-a20d-786aae47b320}</Project> <Project>{5b1171dc-fffe-4813-a20d-786aae47b320}</Project>
<Name>EDMIFileOps</Name> <Name>EDMIFileOps</Name>

View File

@ -1,5 +1,6 @@
Imports System.ServiceModel Imports System.ServiceModel
Imports System.Threading Imports System.Threading
Imports DigitalData.Modules.Config
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Imports EDMI_ClientSuite.NetworkService_DDEDM Imports EDMI_ClientSuite.NetworkService_DDEDM
@ -10,6 +11,7 @@ Namespace My
''' </summary> ''' </summary>
<HideModuleName()> <HideModuleName()>
Module Extension Module Extension
Property ConfigManager As ConfigManager(Of ClassConfig)
Property LogConfig As LogConfig Property LogConfig As LogConfig
Property ChannelFactory As ChannelFactory(Of IEDMServiceChannel) Property ChannelFactory As ChannelFactory(Of IEDMServiceChannel)
Property Channel As IEDMServiceChannel Property Channel As IEDMServiceChannel

View File

@ -20,7 +20,9 @@ Public Class frmConfigService
If oResult = ConnectionTestResult.Successful Then If oResult = ConnectionTestResult.Successful Then
' Save Endpoint URL ' Save Endpoint URL
My.Settings.ICMServiceAddress = oEndpointURL My.ConfigManager.Config.ServiceConnection = oEndpointURL
My.ConfigManager.Save()
'My.Settings.ICMServiceAddress = oEndpointURL
lblStatus.Text = "Verbindung hergestellt." lblStatus.Text = "Verbindung hergestellt."
Else Else
Select Case oResult Select Case oResult

View File

@ -26,7 +26,7 @@ Public NotInheritable Class frmSplash
BringToFront() BringToFront()
Dim oInit As New ClassInit() Dim oInit As New ClassInit()
Dim oConnectionURLExists = oInit.TestConnectionURLExists Dim oConnectionURLExists = oInit.TestConnectionURLExists()
If Not oConnectionURLExists Then If Not oConnectionURLExists Then
Dim oResult = frmConfigService.ShowDialog() Dim oResult = frmConfigService.ShowDialog()

View File

@ -95,6 +95,10 @@ Public Class File
End Sub End Sub
Public Function TestPathIsDirectory(Path As String) As Boolean Public Function TestPathIsDirectory(Path As String) As Boolean
If Not Directory.Exists(Path) Then
Return False
End If
Dim oIsDirectory As Boolean = (System.IO.File.GetAttributes(Path) And FileAttributes.Directory) = FileAttributes.Directory Dim oIsDirectory As Boolean = (System.IO.File.GetAttributes(Path) And FileAttributes.Directory) = FileAttributes.Directory
Return oIsDirectory Return oIsDirectory
End Function End Function