rework Settings in Pilz.Configuration
This commit is contained in:
5
Pilz.Configuration/IChildSettings.vb
Normal file
5
Pilz.Configuration/IChildSettings.vb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
Public Interface IChildSettings
|
||||||
|
|
||||||
|
Sub Reset()
|
||||||
|
|
||||||
|
End Interface
|
||||||
9
Pilz.Configuration/ISettings.vb
Normal file
9
Pilz.Configuration/ISettings.vb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
Public Interface ISettings
|
||||||
|
|
||||||
|
ReadOnly Property Settings As IReadOnlyCollection(Of IChildSettings)
|
||||||
|
|
||||||
|
Function [Get](Of T As IChildSettings)() As T
|
||||||
|
Sub Reset()
|
||||||
|
Sub Initialize(providers As List(Of ISettingsProvider))
|
||||||
|
|
||||||
|
End Interface
|
||||||
7
Pilz.Configuration/ISettingsProvider.vb
Normal file
7
Pilz.Configuration/ISettingsProvider.vb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
Public Interface ISettingsProvider
|
||||||
|
|
||||||
|
Function GetSettingsIdentifiers() As List(Of String)
|
||||||
|
|
||||||
|
Function CreateInstance(identifier As String)
|
||||||
|
|
||||||
|
End Interface
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||||
<Version>2.0.0</Version>
|
<Version>3.0.0</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
|
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
|
||||||
|
|||||||
40
Pilz.Configuration/Settings.vb
Normal file
40
Pilz.Configuration/Settings.vb
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
Imports System.Runtime
|
||||||
|
|
||||||
|
Imports Newtonsoft.Json
|
||||||
|
|
||||||
|
Public Class Settings
|
||||||
|
Implements ISettings
|
||||||
|
|
||||||
|
<JsonProperty(NameOf(Settings))>
|
||||||
|
Private ReadOnly mySettings As New Dictionary(Of String, IChildSettings)
|
||||||
|
|
||||||
|
<JsonIgnore>
|
||||||
|
Public ReadOnly Property Settings As IReadOnlyCollection(Of IChildSettings) Implements ISettings.Settings
|
||||||
|
Get
|
||||||
|
Return mySettings.Values
|
||||||
|
End Get
|
||||||
|
End Property
|
||||||
|
|
||||||
|
Public Function [Get](Of T As IChildSettings)() As T Implements ISettings.Get
|
||||||
|
Return mySettings.Values.OfType(Of T).FirstOrDefault
|
||||||
|
End Function
|
||||||
|
|
||||||
|
Public Sub Reset() Implements ISettings.Reset
|
||||||
|
For Each s In mySettings.Values
|
||||||
|
s.Reset()
|
||||||
|
Next
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
Public Sub Initialize(providers As List(Of ISettingsProvider)) Implements ISettings.Initialize
|
||||||
|
For Each provider In providers
|
||||||
|
For Each identifier In provider.GetSettingsIdentifiers
|
||||||
|
If Not mySettings.ContainsKey(identifier) Then
|
||||||
|
Dim s = provider.CreateInstance(identifier)
|
||||||
|
s.Reset()
|
||||||
|
mySettings.Add(identifier, s)
|
||||||
|
End If
|
||||||
|
Next
|
||||||
|
Next
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
End Class
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
Imports Newtonsoft.Json
|
|
||||||
|
|
||||||
Public MustInherit Class SettingsBase
|
|
||||||
|
|
||||||
Public Sub New()
|
|
||||||
ResetValues()
|
|
||||||
End Sub
|
|
||||||
|
|
||||||
<JsonIgnore>
|
|
||||||
Friend _settingsManager As ISettingsManager
|
|
||||||
<JsonIgnore>
|
|
||||||
Public ReadOnly Property SettingsManager As ISettingsManager
|
|
||||||
Get
|
|
||||||
Return _settingsManager
|
|
||||||
End Get
|
|
||||||
End Property
|
|
||||||
|
|
||||||
Public MustOverride Sub ResetValues()
|
|
||||||
|
|
||||||
End Class
|
|
||||||
@@ -1,20 +1,32 @@
|
|||||||
Imports System.IO
|
Imports System.IO
|
||||||
Imports System.Reflection
|
|
||||||
Imports Newtonsoft.Json.Linq
|
|
||||||
|
|
||||||
Public NotInheritable Class SettingsManager(Of T As SettingsBase)
|
Imports Newtonsoft.Json
|
||||||
|
|
||||||
|
Public Class SettingsManager
|
||||||
Implements ISettingsManager
|
Implements ISettingsManager
|
||||||
|
|
||||||
|
Public Shared Event InitializingManager As EventHandler
|
||||||
|
|
||||||
Public Event AutoSavingSettings As EventHandler
|
Public Event AutoSavingSettings As EventHandler
|
||||||
Public Event SavingSettings As EventHandler
|
Public Event SavingSettings As EventHandler
|
||||||
Public Event SavedSettings As EventHandler
|
Public Event SavedSettings As EventHandler
|
||||||
|
|
||||||
Private defaultInstance As T = Nothing
|
Private ReadOnly settingsProvider As New List(Of ISettingsProvider)
|
||||||
|
Private defaultInstance As ISettings = Nothing
|
||||||
Private enableAutoSave As Boolean = False
|
Private enableAutoSave As Boolean = False
|
||||||
Private addedHandler As Boolean = False
|
Private addedHandler As Boolean = False
|
||||||
|
|
||||||
Public Property ConfigFilePath As String Implements ISettingsManager.ConfigFilePath
|
Public Property ConfigFilePath As String Implements ISettingsManager.ConfigFilePath
|
||||||
|
|
||||||
|
Public ReadOnly Property Instance As IChildSettings
|
||||||
|
Get
|
||||||
|
If defaultInstance Is Nothing Then
|
||||||
|
Load()
|
||||||
|
End If
|
||||||
|
Return defaultInstance
|
||||||
|
End Get
|
||||||
|
End Property
|
||||||
|
|
||||||
Public Property AutoSaveOnExit As Boolean
|
Public Property AutoSaveOnExit As Boolean
|
||||||
Get
|
Get
|
||||||
Return enableAutoSave
|
Return enableAutoSave
|
||||||
@@ -40,25 +52,18 @@ Public NotInheritable Class SettingsManager(Of T As SettingsBase)
|
|||||||
AddHandler AppDomain.CurrentDomain.ProcessExit, AddressOf AutoSaveSettingsOnExit
|
AddHandler AppDomain.CurrentDomain.ProcessExit, AddressOf AutoSaveSettingsOnExit
|
||||||
addedHandler = True
|
addedHandler = True
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Private Sub RemoveAutoSaveHandler()
|
Private Sub RemoveAutoSaveHandler()
|
||||||
RemoveHandler AppDomain.CurrentDomain.ProcessExit, AddressOf AutoSaveSettingsOnExit
|
RemoveHandler AppDomain.CurrentDomain.ProcessExit, AddressOf AutoSaveSettingsOnExit
|
||||||
addedHandler = False
|
addedHandler = False
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Public ReadOnly Property Instance As T
|
|
||||||
Get
|
|
||||||
If defaultInstance Is Nothing Then
|
|
||||||
Load()
|
|
||||||
End If
|
|
||||||
Return defaultInstance
|
|
||||||
End Get
|
|
||||||
End Property
|
|
||||||
|
|
||||||
Public Sub New()
|
Public Sub New()
|
||||||
ConfigFilePath = ""
|
RaiseEvent InitializingManager(Me, EventArgs.Empty)
|
||||||
AutoSaveOnExit = False
|
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Public Sub New(fileName As String, autoSaveOnExit As Boolean)
|
Public Sub New(fileName As String, autoSaveOnExit As Boolean)
|
||||||
|
Me.New
|
||||||
ConfigFilePath = fileName
|
ConfigFilePath = fileName
|
||||||
Me.AutoSaveOnExit = autoSaveOnExit
|
Me.AutoSaveOnExit = autoSaveOnExit
|
||||||
End Sub
|
End Sub
|
||||||
@@ -70,50 +75,44 @@ Public NotInheritable Class SettingsManager(Of T As SettingsBase)
|
|||||||
|
|
||||||
Public Sub Save() Implements ISettingsManager.Save
|
Public Sub Save() Implements ISettingsManager.Save
|
||||||
If Not String.IsNullOrEmpty(ConfigFilePath) AndAlso defaultInstance IsNot Nothing Then
|
If Not String.IsNullOrEmpty(ConfigFilePath) AndAlso defaultInstance IsNot Nothing Then
|
||||||
SavePrivate()
|
SaveInternal()
|
||||||
End If
|
End If
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Public Sub Load() Implements ISettingsManager.Load
|
Public Sub Load() Implements ISettingsManager.Load
|
||||||
If Not String.IsNullOrEmpty(ConfigFilePath) AndAlso File.Exists(ConfigFilePath) Then
|
If Not String.IsNullOrEmpty(ConfigFilePath) AndAlso File.Exists(ConfigFilePath) Then
|
||||||
LoadPrivate()
|
LoadInternal()
|
||||||
Else
|
Else
|
||||||
CreateNewInstance()
|
CreateNewInstance()
|
||||||
End If
|
End If
|
||||||
|
|
||||||
If defaultInstance IsNot Nothing Then
|
|
||||||
defaultInstance._settingsManager = Me
|
|
||||||
End If
|
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Public Sub Reset() Implements ISettingsManager.Reset
|
Public Sub Reset() Implements ISettingsManager.Reset
|
||||||
Instance.ResetValues()
|
Instance.Reset()
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Private Sub CreateNewInstance()
|
Protected Overridable Sub CreateNewInstance()
|
||||||
Dim ctor As ConstructorInfo = GetType(T).GetConstructor({})
|
defaultInstance = New Settings
|
||||||
If ctor IsNot Nothing Then
|
defaultInstance.Initialize(settingsProvider)
|
||||||
defaultInstance = ctor.Invoke({})
|
defaultInstance.Reset()
|
||||||
|
|
||||||
defaultInstance.ResetValues()
|
|
||||||
Else
|
|
||||||
Throw New Exception("The base type has no constructor with no parameters.")
|
|
||||||
End If
|
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Private Sub LoadPrivate()
|
Protected Overridable Sub LoadInternal()
|
||||||
defaultInstance = JObject.Parse(File.ReadAllText(ConfigFilePath)).ToObject(Of T)
|
defaultInstance = JsonConvert.DeserializeObject(Of Settings)(File.ReadAllText(ConfigFilePath), CreateJsonSerializerSettings)
|
||||||
|
defaultInstance.Initialize(settingsProvider)
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Private Sub SavePrivate()
|
Protected Overridable Sub SaveInternal()
|
||||||
RaiseEvent SavingSettings(Me, New EventArgs)
|
RaiseEvent SavingSettings(Me, EventArgs.Empty)
|
||||||
|
File.WriteAllText(ConfigFilePath, JsonConvert.SerializeObject(defaultInstance, CreateJsonSerializerSettings))
|
||||||
Dim obj As JObject = JObject.FromObject(defaultInstance)
|
RaiseEvent SavedSettings(Me, EventArgs.Empty)
|
||||||
If obj IsNot Nothing Then
|
|
||||||
File.WriteAllText(ConfigFilePath, obj.ToString)
|
|
||||||
End If
|
|
||||||
|
|
||||||
RaiseEvent SavedSettings(Me, New EventArgs)
|
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
|
Protected Overridable Function CreateJsonSerializerSettings() As JsonSerializerSettings
|
||||||
|
Return New JsonSerializerSettings With {
|
||||||
|
.Formatting = Formatting.Indented,
|
||||||
|
.TypeNameHandling = TypeNameHandling.Auto
|
||||||
|
}
|
||||||
|
End Function
|
||||||
|
|
||||||
End Class
|
End Class
|
||||||
|
|||||||
Reference in New Issue
Block a user