From 8104dec0e07d01b5bda1a79e78d7700e98a7d0e6 Mon Sep 17 00:00:00 2001 From: schedpas Date: Tue, 14 Jul 2020 13:29:55 +0200 Subject: [PATCH] working on configuration class model --- .../AutoSaveConfigurationManager.vb | 14 +++ Pilz.Configuration/ConfigurationManager.vb | 9 ++ .../ConfigurationManagerList.vb | 83 ++++++++++++++++ Pilz.Configuration/ConfigurationSerializer.vb | 98 ++++++++++++++++--- Pilz.Configuration/Pilz.Configuration.vbproj | 10 +- Pilz.Configuration/SimpleConfiguration.vb | 80 +++++++++++++++ .../SingleInstanceConfiguration.vb | 5 - Pilz/GeneralEventArgs/GetValueEventArgs.vb | 18 ++++ Pilz/Pilz.vbproj | 1 + 9 files changed, 297 insertions(+), 21 deletions(-) create mode 100644 Pilz.Configuration/AutoSaveConfigurationManager.vb create mode 100644 Pilz.Configuration/ConfigurationManager.vb create mode 100644 Pilz.Configuration/ConfigurationManagerList.vb delete mode 100644 Pilz.Configuration/SingleInstanceConfiguration.vb create mode 100644 Pilz/GeneralEventArgs/GetValueEventArgs.vb diff --git a/Pilz.Configuration/AutoSaveConfigurationManager.vb b/Pilz.Configuration/AutoSaveConfigurationManager.vb new file mode 100644 index 0000000..84ebe2a --- /dev/null +++ b/Pilz.Configuration/AutoSaveConfigurationManager.vb @@ -0,0 +1,14 @@ +Public Class AutoSaveConfigurationManager + Inherits ConfigurationManager + + Public Property Enabled As Boolean = False + + Public Sub New() + + End Sub + + Protected Overrides Sub Finalize() + + End Sub + +End Class diff --git a/Pilz.Configuration/ConfigurationManager.vb b/Pilz.Configuration/ConfigurationManager.vb new file mode 100644 index 0000000..772ece6 --- /dev/null +++ b/Pilz.Configuration/ConfigurationManager.vb @@ -0,0 +1,9 @@ +Public MustInherit Class ConfigurationManager + + Public ReadOnly Property Configuration As SimpleConfiguration + + Friend Sub SetConfiguration(configuration As SimpleConfiguration) + _Configuration = configuration + End Sub + +End Class diff --git a/Pilz.Configuration/ConfigurationManagerList.vb b/Pilz.Configuration/ConfigurationManagerList.vb new file mode 100644 index 0000000..c084885 --- /dev/null +++ b/Pilz.Configuration/ConfigurationManagerList.vb @@ -0,0 +1,83 @@ +Imports Pilz.Configuration +Imports Pilz.GeneralEventArgs + +Public Class ConfigurationManagerList + Implements IList(Of ConfigurationManager) + + Public Event GettingParentManager(sender As Object, e As GetValueEventArgs(Of SimpleConfiguration)) + + Private ReadOnly myList As New List(Of ConfigurationManager) + + Private Function GetParentManager() + Dim args As New GetValueEventArgs(Of SimpleConfiguration) + RaiseEvent GettingParentManager(Me, args) + Return args.Value + End Function + + Default Public Property Item(index As Integer) As ConfigurationManager Implements IList(Of ConfigurationManager).Item + Get + Return myList(index) + End Get + Set(value As ConfigurationManager) + myList(index) = value + End Set + End Property + + Public ReadOnly Property Count As Integer Implements ICollection(Of ConfigurationManager).Count + Get + Return myList.Count + End Get + End Property + + Public ReadOnly Property IsReadOnly As Boolean Implements ICollection(Of ConfigurationManager).IsReadOnly + Get + Return False + End Get + End Property + + Public Sub Insert(index As Integer, item As ConfigurationManager) Implements IList(Of ConfigurationManager).Insert + myList.Insert(index, item) + item.SetConfiguration(GetParentManager) + End Sub + + Public Sub RemoveAt(index As Integer) Implements IList(Of ConfigurationManager).RemoveAt + + End Sub + + Public Sub Add(item As ConfigurationManager) Implements ICollection(Of ConfigurationManager).Add + item.SetConfiguration(GetParentManager) + End Sub + + Public Sub Clear() Implements ICollection(Of ConfigurationManager).Clear + For Each item As ConfigurationManager In myList + item.SetConfiguration(Nothing) + Next + myList.Clear() + End Sub + + Public Sub CopyTo(array() As ConfigurationManager, arrayIndex As Integer) Implements ICollection(Of ConfigurationManager).CopyTo + myList.CopyTo(array, arrayIndex) + End Sub + + Public Function IndexOf(item As ConfigurationManager) As Integer Implements IList(Of ConfigurationManager).IndexOf + Return myList.IndexOf(item) + End Function + + Public Function Contains(item As ConfigurationManager) As Boolean Implements ICollection(Of ConfigurationManager).Contains + Return myList.Contains(item) + End Function + + Public Function Remove(item As ConfigurationManager) As Boolean Implements ICollection(Of ConfigurationManager).Remove + item.SetConfiguration(Nothing) + Return myList.Remove(item) + End Function + + Public Function GetEnumerator() As IEnumerator(Of ConfigurationManager) Implements IEnumerable(Of ConfigurationManager).GetEnumerator + Return IEnumerable_GetEnumerator() + End Function + + Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator + Return myList.GetEnumerator + End Function + +End Class diff --git a/Pilz.Configuration/ConfigurationSerializer.vb b/Pilz.Configuration/ConfigurationSerializer.vb index dfeaded..6eb860f 100644 --- a/Pilz.Configuration/ConfigurationSerializer.vb +++ b/Pilz.Configuration/ConfigurationSerializer.vb @@ -1,22 +1,35 @@ Imports System.IO Imports Newtonsoft.Json +Imports Pilz.GeneralEventArgs -Friend Module ConfigurationSerializer +Public Module ConfigurationSerializer + + Public Event GettingJsonSerializer(instance As Object, e As GetValueEventArgs(Of JsonSerializer)) + + Private Function GetJsonSerializer(instance As SimpleConfiguration) As JsonSerializer + Dim args As New GetValueEventArgs(Of JsonSerializer)(JsonSerializer.CreateDefault) + RaiseEvent GettingJsonSerializer(instance, args) + Return args.Value + End Function ''' ''' Writes the given instance to a string and return it. ''' - ''' - ''' + ''' The configuration instance that should be serialized. + ''' The content of the configuration instance as string. Public Function WriteToString(instance As SimpleConfiguration) As String - Return JsonConvert.SerializeObject(instance) + Dim tw As New StringWriter + GetJsonSerializer(instance).Serialize(tw, instance) + Dim txt = tw.ToString + tw.Close() + Return txt End Function ''' ''' Write the given instance to a given stream. ''' - ''' - ''' + ''' The configuration instance that should be serialized. + ''' The stream where the content should be written to. Public Sub WriteToStream(instance As SimpleConfiguration, stream As Stream) Dim sr As New StreamWriter(stream) sr.Write(WriteToString(instance)) @@ -25,10 +38,10 @@ Friend Module ConfigurationSerializer ''' ''' Writes the given instance to the given filePath as text file. ''' - ''' - ''' + ''' The configuration instance that should be serialized. + ''' The file path where the content should be written to. The file will be created or overwritten. Public Sub WriteToFile(instance As SimpleConfiguration, filePath As String) - Dim fs As New FileStream(filePath, FileMode.Create) + Dim fs As New FileStream(filePath, FileMode.Create, FileAccess.ReadWrite) WriteToStream(instance, fs) fs.Close() End Sub @@ -36,20 +49,75 @@ Friend Module ConfigurationSerializer ''' ''' Reads a configuratin from the given string and returns an instance of it. ''' - ''' - ''' + ''' The type of the configuration class to instance. + ''' The content of the configuration as string. ''' Public Function ReadFromString(Of T As SimpleConfiguration)(content As String) As T - Return JsonConvert.DeserializeObject(Of T)(content) + Dim sr As New StringReader(content) + Dim instance As T = GetJsonSerializer(Nothing).Deserialize(sr, GetType(T)) + sr.Close() + Return instance End Function ''' ''' Read a configuration from the given string and put them to the given instance. ''' - ''' - ''' + ''' The instance to populate with the configuration. + ''' The content of the configuration as string. Public Sub ReadFromString(instance As SimpleConfiguration, content As String) - JsonConvert.PopulateObject(content, instance) + Dim sr As New StringReader(content) + GetJsonSerializer(Nothing).Populate(sr, content) + sr.Close() End Sub + ''' + ''' Reads a configuratin from the given string and returns an instance of it. + ''' + ''' The type of the configuration class to instance. + ''' The stream with the content of the configuration. + ''' + Public Function ReadFromStream(Of T As SimpleConfiguration)(stream As Stream) As T + Return ReadFromString(Of T)(GetContentOfStream(stream)) + End Function + + ''' + ''' Read a configuration from the given string and put them to the given instance. + ''' + ''' The instance to populate with the configuration. + ''' The stream with the content of the configuration. + Public Sub ReadFromStream(instance As SimpleConfiguration, stream As Stream) + ReadFromString(instance, GetContentOfStream(stream)) + End Sub + + ''' + ''' Reads a configuratin from the given string and returns an instance of it. + ''' + ''' The type of the configuration class to instance. + ''' The path to the file with the content of the configuration. + ''' + Public Function ReadFromFile(Of T As SimpleConfiguration)(filePath As String) As T + Return ReadFromString(Of T)(GetContentOfFile(filePath)) + End Function + + ''' + ''' Read a configuration from the given string and put them to the given instance. + ''' + ''' The instance to populate with the configuration. + ''' The path to the file with the content of the configuration. + Public Sub ReadFromFile(instance As SimpleConfiguration, filePath As String) + ReadFromString(instance, GetContentOfFile(filePath)) + End Sub + + Private Function GetContentOfStream(stream As Stream) As String + Dim sr As New StreamReader(stream) + Return sr.ReadToEnd + End Function + + Private Function GetContentOfFile(filePath As String) As String + Dim fs As New FileStream(filePath, FileMode.Open, FileAccess.Read) + Dim content = GetContentOfStream(fs) + fs.Close() + Return content + End Function + End Module diff --git a/Pilz.Configuration/Pilz.Configuration.vbproj b/Pilz.Configuration/Pilz.Configuration.vbproj index 3252bbf..ada218a 100644 --- a/Pilz.Configuration/Pilz.Configuration.vbproj +++ b/Pilz.Configuration/Pilz.Configuration.vbproj @@ -69,8 +69,10 @@ + + - + @@ -111,5 +113,11 @@ + + + {277D2B83-7613-4C49-9CAB-E080195A6E0C} + Pilz + + \ No newline at end of file diff --git a/Pilz.Configuration/SimpleConfiguration.vb b/Pilz.Configuration/SimpleConfiguration.vb index 7cc5042..2dd432f 100644 --- a/Pilz.Configuration/SimpleConfiguration.vb +++ b/Pilz.Configuration/SimpleConfiguration.vb @@ -1,19 +1,99 @@ Imports System.IO +Imports Newtonsoft.Json +Imports Pilz.Configuration +Imports Pilz.GeneralEventArgs Public Class SimpleConfiguration + + Public ReadOnly Managers As New ConfigurationManagerList + Public Sub New() + AddHandler Managers.GettingParentManager, AddressOf Managers_GettingParentManager + End Sub + Private Sub Managers_GettingParentManager(sender As Object, e As GetValueEventArgs(Of SimpleConfiguration)) + If sender Is Managers Then + e.Value = Me + End If + End Sub + + ''' + ''' Writes this instance to a string and return it. + ''' + ''' The content of the configuration instance as string. Public Function WriteToString() As String Return ConfigurationSerializer.WriteToString(Me) End Function + ''' + ''' Write this instance to a given stream. + ''' + ''' The stream where the content should be written to. Public Sub WriteToStream(stream As Stream) ConfigurationSerializer.WriteToStream(Me, stream) End Sub + ''' + ''' Writes this instance to the given filePath as text file. + ''' + ''' The file path where the content should be written to. The file will be created or overwritten. Public Sub WriteToFile(filePath As String) ConfigurationSerializer.WriteToFile(Me, filePath) End Sub + ''' + ''' Reads a configuratin from the given string and returns an instance of it. + ''' + ''' The type of the configuration class to instance. + ''' The content of the configuration as string. + ''' + Public Shared Function ReadFromString(Of T As SimpleConfiguration)(content As String) As T + Return ConfigurationSerializer.ReadFromString(Of T)(content) + End Function + + ''' + ''' Read a configuration from the given string and put them to this instance. + ''' + ''' The content of the configuration as string. + Public Sub ReadFromString(content As String) + ConfigurationSerializer.ReadFromString(Me, content) + End Sub + + ''' + ''' Reads a configuratin from the given string and returns an instance of it. + ''' + ''' The type of the configuration class to instance. + ''' The stream with the content of the configuration. + ''' + Public Shared Function ReadFromStream(Of T As SimpleConfiguration)(stream As Stream) As T + Return ConfigurationSerializer.ReadFromStream(Of T)(stream) + End Function + + ''' + ''' Read a configuration from the given string and put them to this instance. + ''' + ''' The stream with the content of the configuration. + Public Sub ReadFromStream(stream As Stream) + ConfigurationSerializer.ReadFromStream(Me, stream) + End Sub + + ''' + ''' Reads a configuratin from the given string and returns an instance of it. + ''' + ''' The type of the configuration class to instance. + ''' The path to the file with the content of the configuration. + ''' + Public Shared Function ReadFromFile(Of T As SimpleConfiguration)(filePath As String) As T + Return ConfigurationSerializer.ReadFromFile(Of T)(filePath) + End Function + + ''' + ''' Read a configuration from the given string and put them to this instance. + ''' + ''' The path to the file with the content of the configuration. + Public Sub ReadFromFile(filePath As String) + ConfigurationSerializer.ReadFromFile(Me, filePath) + End Sub + End Class diff --git a/Pilz.Configuration/SingleInstanceConfiguration.vb b/Pilz.Configuration/SingleInstanceConfiguration.vb deleted file mode 100644 index 9678fe1..0000000 --- a/Pilz.Configuration/SingleInstanceConfiguration.vb +++ /dev/null @@ -1,5 +0,0 @@ -Public Class SingleInstanceConfiguration - Inherits SimpleConfiguration - - -End Class diff --git a/Pilz/GeneralEventArgs/GetValueEventArgs.vb b/Pilz/GeneralEventArgs/GetValueEventArgs.vb new file mode 100644 index 0000000..397e2ed --- /dev/null +++ b/Pilz/GeneralEventArgs/GetValueEventArgs.vb @@ -0,0 +1,18 @@ +Namespace GeneralEventArgs + + Public Class GetValueEventArgs(Of T) + Inherits EventArgs + + Public Property Value As T + + Public Sub New() + MyBase.New + End Sub + + Public Sub New(value As T) + MyBase.New + End Sub + + End Class + +End Namespace diff --git a/Pilz/Pilz.vbproj b/Pilz/Pilz.vbproj index 5dd33dc..daca82a 100644 --- a/Pilz/Pilz.vbproj +++ b/Pilz/Pilz.vbproj @@ -66,6 +66,7 @@ +