working on configuration class model
This commit is contained in:
14
Pilz.Configuration/AutoSaveConfigurationManager.vb
Normal file
14
Pilz.Configuration/AutoSaveConfigurationManager.vb
Normal file
@@ -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
|
||||||
9
Pilz.Configuration/ConfigurationManager.vb
Normal file
9
Pilz.Configuration/ConfigurationManager.vb
Normal file
@@ -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
|
||||||
83
Pilz.Configuration/ConfigurationManagerList.vb
Normal file
83
Pilz.Configuration/ConfigurationManagerList.vb
Normal file
@@ -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
|
||||||
@@ -1,22 +1,35 @@
|
|||||||
Imports System.IO
|
Imports System.IO
|
||||||
Imports Newtonsoft.Json
|
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
|
||||||
|
|
||||||
''' <summary>
|
''' <summary>
|
||||||
''' Writes the given instance to a string and return it.
|
''' Writes the given instance to a string and return it.
|
||||||
''' </summary>
|
''' </summary>
|
||||||
''' <param name="instance"></param>
|
''' <param name="instance">The configuration instance that should be serialized.</param>
|
||||||
''' <returns></returns>
|
''' <returns>The content of the configuration instance as string.</returns>
|
||||||
Public Function WriteToString(instance As SimpleConfiguration) 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
|
End Function
|
||||||
|
|
||||||
''' <summary>
|
''' <summary>
|
||||||
''' Write the given instance to a given stream.
|
''' Write the given instance to a given stream.
|
||||||
''' </summary>
|
''' </summary>
|
||||||
''' <param name="instance"></param>
|
''' <param name="instance">The configuration instance that should be serialized.</param>
|
||||||
''' <param name="stream"></param>
|
''' <param name="stream">The stream where the content should be written to.</param>
|
||||||
Public Sub WriteToStream(instance As SimpleConfiguration, stream As Stream)
|
Public Sub WriteToStream(instance As SimpleConfiguration, stream As Stream)
|
||||||
Dim sr As New StreamWriter(stream)
|
Dim sr As New StreamWriter(stream)
|
||||||
sr.Write(WriteToString(instance))
|
sr.Write(WriteToString(instance))
|
||||||
@@ -25,10 +38,10 @@ Friend Module ConfigurationSerializer
|
|||||||
''' <summary>
|
''' <summary>
|
||||||
''' Writes the given instance to the given filePath as text file.
|
''' Writes the given instance to the given filePath as text file.
|
||||||
''' </summary>
|
''' </summary>
|
||||||
''' <param name="instance"></param>
|
''' <param name="instance">The configuration instance that should be serialized.</param>
|
||||||
''' <param name="filePath"></param>
|
''' <param name="filePath">The file path where the content should be written to. The file will be created or overwritten.</param>
|
||||||
Public Sub WriteToFile(instance As SimpleConfiguration, filePath As String)
|
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)
|
WriteToStream(instance, fs)
|
||||||
fs.Close()
|
fs.Close()
|
||||||
End Sub
|
End Sub
|
||||||
@@ -36,20 +49,75 @@ Friend Module ConfigurationSerializer
|
|||||||
''' <summary>
|
''' <summary>
|
||||||
''' Reads a configuratin from the given string and returns an instance of it.
|
''' Reads a configuratin from the given string and returns an instance of it.
|
||||||
''' </summary>
|
''' </summary>
|
||||||
''' <typeparam name="T"></typeparam>
|
''' <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||||
''' <param name="content"></param>
|
''' <param name="content">The content of the configuration as string.</param>
|
||||||
''' <returns></returns>
|
''' <returns></returns>
|
||||||
Public Function ReadFromString(Of T As SimpleConfiguration)(content As String) As T
|
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
|
End Function
|
||||||
|
|
||||||
''' <summary>
|
''' <summary>
|
||||||
''' Read a configuration from the given string and put them to the given instance.
|
''' Read a configuration from the given string and put them to the given instance.
|
||||||
''' </summary>
|
''' </summary>
|
||||||
''' <param name="instance"></param>
|
''' <param name="instance">The instance to populate with the configuration.</param>
|
||||||
''' <param name="content"></param>
|
''' <param name="content">The content of the configuration as string.</param>
|
||||||
Public Sub ReadFromString(instance As SimpleConfiguration, content 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
|
End Sub
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Reads a configuratin from the given string and returns an instance of it.
|
||||||
|
''' </summary>
|
||||||
|
''' <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||||
|
''' <param name="stream">The stream with the content of the configuration.</param>
|
||||||
|
''' <returns></returns>
|
||||||
|
Public Function ReadFromStream(Of T As SimpleConfiguration)(stream As Stream) As T
|
||||||
|
Return ReadFromString(Of T)(GetContentOfStream(stream))
|
||||||
|
End Function
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Read a configuration from the given string and put them to the given instance.
|
||||||
|
''' </summary>
|
||||||
|
''' <param name="instance">The instance to populate with the configuration.</param>
|
||||||
|
''' <param name="stream">The stream with the content of the configuration.</param>
|
||||||
|
Public Sub ReadFromStream(instance As SimpleConfiguration, stream As Stream)
|
||||||
|
ReadFromString(instance, GetContentOfStream(stream))
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Reads a configuratin from the given string and returns an instance of it.
|
||||||
|
''' </summary>
|
||||||
|
''' <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||||
|
''' <param name="filePath">The path to the file with the content of the configuration.</param>
|
||||||
|
''' <returns></returns>
|
||||||
|
Public Function ReadFromFile(Of T As SimpleConfiguration)(filePath As String) As T
|
||||||
|
Return ReadFromString(Of T)(GetContentOfFile(filePath))
|
||||||
|
End Function
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Read a configuration from the given string and put them to the given instance.
|
||||||
|
''' </summary>
|
||||||
|
''' <param name="instance">The instance to populate with the configuration.</param>
|
||||||
|
''' <param name="filePath">The path to the file with the content of the configuration.</param>
|
||||||
|
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
|
End Module
|
||||||
|
|||||||
@@ -69,8 +69,10 @@
|
|||||||
<Import Include="System.Threading.Tasks" />
|
<Import Include="System.Threading.Tasks" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="ConfigurationManager.vb" />
|
||||||
|
<Compile Include="ConfigurationManagerList.vb" />
|
||||||
<Compile Include="ConfigurationSerializer.vb" />
|
<Compile Include="ConfigurationSerializer.vb" />
|
||||||
<Compile Include="SingleInstanceConfiguration.vb" />
|
<Compile Include="AutoSaveConfigurationManager.vb" />
|
||||||
<Compile Include="SimpleConfiguration.vb" />
|
<Compile Include="SimpleConfiguration.vb" />
|
||||||
<Compile Include="ISettingsManager.vb" />
|
<Compile Include="ISettingsManager.vb" />
|
||||||
<Compile Include="My Project\AssemblyInfo.vb" />
|
<Compile Include="My Project\AssemblyInfo.vb" />
|
||||||
@@ -111,5 +113,11 @@
|
|||||||
</None>
|
</None>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Pilz\Pilz.vbproj">
|
||||||
|
<Project>{277D2B83-7613-4C49-9CAB-E080195A6E0C}</Project>
|
||||||
|
<Name>Pilz</Name>
|
||||||
|
</ProjectReference>
|
||||||
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,19 +1,99 @@
|
|||||||
Imports System.IO
|
Imports System.IO
|
||||||
|
Imports Newtonsoft.Json
|
||||||
|
Imports Pilz.Configuration
|
||||||
|
Imports Pilz.GeneralEventArgs
|
||||||
|
|
||||||
Public Class SimpleConfiguration
|
Public Class SimpleConfiguration
|
||||||
|
|
||||||
|
<JsonIgnore>
|
||||||
|
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
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Writes this instance to a string and return it.
|
||||||
|
''' </summary>
|
||||||
|
''' <returns>The content of the configuration instance as string.</returns>
|
||||||
Public Function WriteToString() As String
|
Public Function WriteToString() As String
|
||||||
Return ConfigurationSerializer.WriteToString(Me)
|
Return ConfigurationSerializer.WriteToString(Me)
|
||||||
End Function
|
End Function
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Write this instance to a given stream.
|
||||||
|
''' </summary>
|
||||||
|
''' <param name="stream">The stream where the content should be written to.</param>
|
||||||
Public Sub WriteToStream(stream As Stream)
|
Public Sub WriteToStream(stream As Stream)
|
||||||
ConfigurationSerializer.WriteToStream(Me, stream)
|
ConfigurationSerializer.WriteToStream(Me, stream)
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Writes this instance to the given filePath as text file.
|
||||||
|
''' </summary>
|
||||||
|
''' <param name="filePath">The file path where the content should be written to. The file will be created or overwritten.</param>
|
||||||
Public Sub WriteToFile(filePath As String)
|
Public Sub WriteToFile(filePath As String)
|
||||||
ConfigurationSerializer.WriteToFile(Me, filePath)
|
ConfigurationSerializer.WriteToFile(Me, filePath)
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Reads a configuratin from the given string and returns an instance of it.
|
||||||
|
''' </summary>
|
||||||
|
''' <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||||
|
''' <param name="content">The content of the configuration as string.</param>
|
||||||
|
''' <returns></returns>
|
||||||
|
Public Shared Function ReadFromString(Of T As SimpleConfiguration)(content As String) As T
|
||||||
|
Return ConfigurationSerializer.ReadFromString(Of T)(content)
|
||||||
|
End Function
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Read a configuration from the given string and put them to this instance.
|
||||||
|
''' </summary>
|
||||||
|
''' <param name="content">The content of the configuration as string.</param>
|
||||||
|
Public Sub ReadFromString(content As String)
|
||||||
|
ConfigurationSerializer.ReadFromString(Me, content)
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Reads a configuratin from the given string and returns an instance of it.
|
||||||
|
''' </summary>
|
||||||
|
''' <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||||
|
''' <param name="stream">The stream with the content of the configuration.</param>
|
||||||
|
''' <returns></returns>
|
||||||
|
Public Shared Function ReadFromStream(Of T As SimpleConfiguration)(stream As Stream) As T
|
||||||
|
Return ConfigurationSerializer.ReadFromStream(Of T)(stream)
|
||||||
|
End Function
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Read a configuration from the given string and put them to this instance.
|
||||||
|
''' </summary>
|
||||||
|
''' <param name="stream">The stream with the content of the configuration.</param>
|
||||||
|
Public Sub ReadFromStream(stream As Stream)
|
||||||
|
ConfigurationSerializer.ReadFromStream(Me, stream)
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Reads a configuratin from the given string and returns an instance of it.
|
||||||
|
''' </summary>
|
||||||
|
''' <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||||
|
''' <param name="filePath">The path to the file with the content of the configuration.</param>
|
||||||
|
''' <returns></returns>
|
||||||
|
Public Shared Function ReadFromFile(Of T As SimpleConfiguration)(filePath As String) As T
|
||||||
|
Return ConfigurationSerializer.ReadFromFile(Of T)(filePath)
|
||||||
|
End Function
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Read a configuration from the given string and put them to this instance.
|
||||||
|
''' </summary>
|
||||||
|
''' <param name="filePath">The path to the file with the content of the configuration.</param>
|
||||||
|
Public Sub ReadFromFile(filePath As String)
|
||||||
|
ConfigurationSerializer.ReadFromFile(Me, filePath)
|
||||||
|
End Sub
|
||||||
|
|
||||||
End Class
|
End Class
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
Public Class SingleInstanceConfiguration
|
|
||||||
Inherits SimpleConfiguration
|
|
||||||
|
|
||||||
|
|
||||||
End Class
|
|
||||||
18
Pilz/GeneralEventArgs/GetValueEventArgs.vb
Normal file
18
Pilz/GeneralEventArgs/GetValueEventArgs.vb
Normal file
@@ -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
|
||||||
@@ -66,6 +66,7 @@
|
|||||||
<Import Include="System.Threading.Tasks" />
|
<Import Include="System.Threading.Tasks" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="GeneralEventArgs\GetValueEventArgs.vb" />
|
||||||
<Compile Include="HelpfulFunctions.vb" />
|
<Compile Include="HelpfulFunctions.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">
|
||||||
|
|||||||
Reference in New Issue
Block a user