starting with new configuration class model
This commit is contained in:
55
Pilz.Configuration/ConfigurationSerializer.vb
Normal file
55
Pilz.Configuration/ConfigurationSerializer.vb
Normal file
@@ -0,0 +1,55 @@
|
||||
Imports System.IO
|
||||
Imports Newtonsoft.Json
|
||||
|
||||
Friend Module ConfigurationSerializer
|
||||
|
||||
''' <summary>
|
||||
''' Writes the given instance to a string and return it.
|
||||
''' </summary>
|
||||
''' <param name="instance"></param>
|
||||
''' <returns></returns>
|
||||
Public Function WriteToString(instance As SimpleConfiguration) As String
|
||||
Return JsonConvert.SerializeObject(instance)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Write the given instance to a given stream.
|
||||
''' </summary>
|
||||
''' <param name="instance"></param>
|
||||
''' <param name="stream"></param>
|
||||
Public Sub WriteToStream(instance As SimpleConfiguration, stream As Stream)
|
||||
Dim sr As New StreamWriter(stream)
|
||||
sr.Write(WriteToString(instance))
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' Writes the given instance to the given filePath as text file.
|
||||
''' </summary>
|
||||
''' <param name="instance"></param>
|
||||
''' <param name="filePath"></param>
|
||||
Public Sub WriteToFile(instance As SimpleConfiguration, filePath As String)
|
||||
Dim fs As New FileStream(filePath, FileMode.Create)
|
||||
WriteToStream(instance, fs)
|
||||
fs.Close()
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' Reads a configuratin from the given string and returns an instance of it.
|
||||
''' </summary>
|
||||
''' <typeparam name="T"></typeparam>
|
||||
''' <param name="content"></param>
|
||||
''' <returns></returns>
|
||||
Public Function ReadFromString(Of T As SimpleConfiguration)(content As String) As T
|
||||
Return JsonConvert.DeserializeObject(Of T)(content)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Read a configuration from the given string and put them to the given instance.
|
||||
''' </summary>
|
||||
''' <param name="instance"></param>
|
||||
''' <param name="content"></param>
|
||||
Public Sub ReadFromString(instance As SimpleConfiguration, content As String)
|
||||
JsonConvert.PopulateObject(content, instance)
|
||||
End Sub
|
||||
|
||||
End Module
|
||||
@@ -69,6 +69,9 @@
|
||||
<Import Include="System.Threading.Tasks" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ConfigurationSerializer.vb" />
|
||||
<Compile Include="SingleInstanceConfiguration.vb" />
|
||||
<Compile Include="SimpleConfiguration.vb" />
|
||||
<Compile Include="ISettingsManager.vb" />
|
||||
<Compile Include="My Project\AssemblyInfo.vb" />
|
||||
<Compile Include="My Project\Application.Designer.vb">
|
||||
|
||||
19
Pilz.Configuration/SimpleConfiguration.vb
Normal file
19
Pilz.Configuration/SimpleConfiguration.vb
Normal file
@@ -0,0 +1,19 @@
|
||||
Imports System.IO
|
||||
|
||||
Public Class SimpleConfiguration
|
||||
|
||||
|
||||
|
||||
Public Function WriteToString() As String
|
||||
Return ConfigurationSerializer.WriteToString(Me)
|
||||
End Function
|
||||
|
||||
Public Sub WriteToStream(stream As Stream)
|
||||
ConfigurationSerializer.WriteToStream(Me, stream)
|
||||
End Sub
|
||||
|
||||
Public Sub WriteToFile(filePath As String)
|
||||
ConfigurationSerializer.WriteToFile(Me, filePath)
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
5
Pilz.Configuration/SingleInstanceConfiguration.vb
Normal file
5
Pilz.Configuration/SingleInstanceConfiguration.vb
Normal file
@@ -0,0 +1,5 @@
|
||||
Public Class SingleInstanceConfiguration
|
||||
Inherits SimpleConfiguration
|
||||
|
||||
|
||||
End Class
|
||||
Reference in New Issue
Block a user